# How would I align a model to a slope

546
5
02-10-2013 06:41 AM
Occasional Contributor
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?

Thanks!
Tags (3)
5 Replies
by Anonymous User
Not applicable
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.
Frequent Contributor
Hi !

Shapes can be aligned to Terrains and Terrains back to Shapes.

Models are ONLY generated with CGA, BASED ON the Shape. So there's no way to align the Terrain to a Model or the other way round in CE.

The best you can get is a proper alignment between Terrain and Shape.

On the CGA side though, of course you have multiple ways to generate Models which accentuate the fact that the shapes is actually not flat, but sloped.

E.g. by creating a flat base first on which you build a flat building or to extrude directly and use all vertical facades with a sloped 'cut' at the bottom.

Hope this helps ..

m.
by Anonymous User
Not applicable
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.

[ATTACH=CONFIG]21696[/ATTACH]
New Contributor III
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.
Frequent Contributor
in CGA :
geometry.angle(azimuth)

check the CGA Reference for a little example.

m.