#declare PylonHighness = 2.95;
#declare PylonRadius = 0.075;
#declare WireWidth = 0.02;
#declare WireFencingLength = 4;
#declare WireInterval = 0.1;
#declare FencesWiresHighness = PylonHighness * 0.95;

#declare WestFenceStartingPos = < min_extent( NorthForestEarthBox ).x, max_extent( NorthForestEarthBox ).y, min_extent( NorthForestEarthBox ).z >;
#declare WestFenceEndingPos = < max_extent( NorthForestEarthBox ).x, max_extent( NorthForestEarthBox ).y, min_extent( NorthForestEarthBox ).z >;

#declare WestFencesNumber = int( ( WestFenceEndingPos.x - WestFenceStartingPos.x ) / WireFencingLength );
#declare EastSideRoadFencesNumber = int( ( max_extent( EastSideRoad ).z - min_extent( EastSideRoad ).z ) / WireFencingLength );
#declare WestSideRoadFencesNumber = int( ( max_extent( NorthForestEarthBox ).z - min_extent( NorthForestEarthBox ).z ) / WireFencingLength );

#declare PylonColor = color srgb 0.6;
#declare PylonFinish = finish{ specular 0.1 roughness 0.01 }
#declare PylonTexture = texture{ pigment{ PylonColor } finish{ PylonFinish } }

#declare WireColor = color srgb 0.5;
#declare WireFinish = finish{ specular 0.01 roughness 0.01 }
#declare WireTexture = texture{ pigment{ WireColor } finish{ WireFinish } }

#declare CompletePylon =
union{
	cylinder{ 0, < 0, PylonHighness, 0 >, PylonRadius }
	sphere{< 0, PylonHighness, 0 >, PylonRadius }
	texture{ PylonTexture }
}

#macro MakeWireFenceOnX(SingleFenceLength, SingleFencesNumber, WireFenceTopMax, WireSpacing, Transform)
	union{
		#for( It, 0, SingleFencesNumber - 1 )
			MakeFenceTrunkWire(SingleFenceLength, It, WireFenceTopMax, WireSpacing, transform{ translate < SingleFenceLength * It, 0, WireWidth / 2 > } )
			MakeFenceTrunkWire(SingleFenceLength, It, WireFenceTopMax, WireSpacing, transform{ rotate 180 * y translate < SingleFenceLength * ( It + 1 ), 0, -WireWidth / 2 > } )
			object{ CompletePylon translate It * SingleFenceLength * x texture{ PylonTexture } }
		#end
		object{ CompletePylon translate SingleFencesNumber * SingleFenceLength * x texture{ PylonTexture } }
		transform{ Transform }
	}
#end

#macro MakeFenceTrunkWire(SingleFenceLength, FenceNumber, WireFenceTopMax, WireSpacing, Transform)
	#local WiresNumber = SingleFenceLength / WireSpacing;
	union{
		#for( It, 0, WiresNumber - 1 )
			#if( SingleFenceLength - It * WireSpacing < WireFenceTopMax )
				cylinder{ < It * WireSpacing, 0, 0 >, < SingleFenceLength, SingleFenceLength - It * WireSpacing, 0 >, WireWidth / 2 texture{ WireTexture } }
			#else
				cylinder{ < It * WireSpacing, 0, 0 >, < It * WireSpacing + WireFenceTopMax, WireFenceTopMax, 0 >, WireWidth / 2 texture{ WireTexture } }
			#end
		#end
		#for( It, 0, WireFenceTopMax / WireSpacing - 1 )
			cylinder{ < 0, It * WireSpacing, 0 >, < WireFenceTopMax - It * WireSpacing, WireFenceTopMax, 0 >, WireWidth / 2 texture{ WireTexture } }
		#end
		transform{ Transform }	
	}
#end