I've got an object that I want to align to the slope of the terrain either facing down or up the slope or aligned along it (for example along a contour). Is there a bit of code that can tell me which way is up and down on a surface? I know you can assess slope angle of a surface can it be made to tell me the direction?
I don't see a built in way in CGA. In python, you could take the point of insertion and sample the heightmap at three nearby points (like forming a little triangle shape around the point). From three points you can get a plane (mathematically), and hence a normal vector too. You could store the normal vector as an object attribute on your point shape (or as three attributes for x,y,z, I guess). You'd want to first test to see if the vector points up or down (is z negative?) and reverse the vector if so.
To find the contour-aligned vector, you can use the cross product of the surface normal and a z unit vector, and for the upslope direction vector you cross that vector with the surface normal. But this is somewhat complicated... and I don't remember how to do cross products by hand anymore.
As Matt said "best you can get is a proper alignment between Terrain and Shape". So we can use Python for generating a shape that is properly aligned to point upslope and along contours. I cobbled together a script which takes point data, and projects them onto a terrain, and creates rectangles at the appropriate orientation on a new shape layer. The U direction is walking straight up the max slope, and the V direction is walking the contour lines (or vice versa). You can see the rectangles in the image below. If this is what you are looking for I'll clean up the code and post it here.
Is there a bit of code that can tell me which way is up and down on a surface? I know you can assess slope angle of a surface can it be made to tell me the direction?
If you are working with a raster topographic surface and have access to Spatial Analyst... a less dynamic but workable option would be to use the Aspect operation in spatial analyst to create a raster where cell values indicate direction of the slope in degrees (0-360, with 0 = North). The direction the operation provides is for down slope; up slope could be obtained by adding or subtracting 180; cross slope (perpendicular to down or up slope) by adding or subtracting 90.
You could bring in one or several aspect rasters ('downSlope', 'upSlope', 'crossSlope') into CE as map layers. Then use a cga rule applied to the objects to: - get the orientation in degrees from desired map layer (the map layer used could be exposed as an attribute) - use the value from the map layer to rotate the object the number of degrees that would make it face downSlope, upSlope, or crossSlope.
This would require the imported objects to all be facing north, prior to reorientation.