-
Notifications
You must be signed in to change notification settings - Fork 1
Making a New Object With a Geometry and a Coordinate System
This page will outline a basic approach to how polyhedrons and coordinate systems can be used in a higher level library to represent objects in space. This is just one way, but there may be many other just as "good" ways to do this. It is also important to note that this example uses IShiftable and IShiftableCollection, which are two interfaces in this Library that are already implemented that handle shifting as described here and follows their structure. We also realize that this is not the only way shifting can be done, but it is the way it is intended to be done with this library and it helps avoid cumulative errors from repeated shifts. For this page, we will overview how you can make a simple shape, a lego block, and a compound shape, a lego set. In this example we will say the lego block has a rectangular geometry and that the lego set is a collection of lego blocks. First we will look at what the lego block class would need to have: • A Geometry - for this example a Polyhedron. This should be a private class that has the geometry oriented in its "home" coordinate system. There should then be a getter method that returns the geometry shifted based on its home coordinate system to the current system (i.e. geometry.Shift(homeSystem.shiftFromThisTo(currentSystem)))) but this should leave the geometry stored in lego block alone. By doing this, accuracy errors will not build up over time by repeatedly shifting the geometry. • A "Home" CoordinateSystem - this will keep track of where the geometry is in relation to the world coordinates and the geometry will be aligned with the coordinates in this system. • A "Current" CoordinateSystem - this will keep track of what coordinate system to display the geometry in. When making a constructor for lego block, the geometry should be created in its own coordinate system, based at 0,0,0. If the object is to be moved, this is accomplished by changing the home coordinate system and NOT shifting the geometry itself (even in the constructors). This will have no "immediate" effect on the geometry, but when you call the getter method for it, it will shift the geometry based on its home system. This also means that you cannot "hold" onto the geometry's reference if the geometry will be changing and being shifted, because it will only "update" the geometry when the getter is called and doe not dynamically update it. For more on shifting in this example see the page on shifting an object with geometry and a coordinate system.
Example lego block class:
public class LegoBlock : IShiftable
{
//The geometry that can only be set internally and should always stay in its home coordinate system, only shifting copies of it when we get the geometry out of this class
private Polyhedron _geometry;
public Geometry
{
get { return _geometry.Shift(this.HomeCoordinateSystem.ShiftFromThisTo(CurrentCoordinateSystem)); }
protected set { _geometry = value; }
}
//IShiftable properties
public CoordinateSystem HomeCoordinateSystem;
public CoordinateSystem CurrentCoordinateSystem;
//we do not need any implementation for IShfitable
//other methods
...
}
Now we can look at a composite class, lego sets, which contains a list of lego blocks. This class should be straight forward for the most part, it will contain: • List of LegoBlocks - This list should contain all of the lego blocks that are part of this lego set • A "Set" Coordinate System - This is a coordinate system that positions the set in the world coordinates • A "Current" CoordinateSystem - this will keep track of which coordinate system the lego set should be in (note that this can get confusing since you can shift each block's current system separately, but it is used when you want to position the whole set and if the shift is implemented as per the shifting page, it will move all its lego blocks into the set's current coordinate system) This object simple keeps track of a "view" of the lego blocks that goes with this set and also allows the user to shift all of the lego blocks in the set at the same time, or any other operations that would like to be applied to the whole set. Shifting this can be a bit trickier than a simple shape like lego block. To see how to do this with this same example see the same page on shifting an object with geometry and a coordinate system.
Example lego set class:
public class LegoSet : IShiftableCollection
{
//our collections in this object (could have multiple)
public List<LegoBlock> LegoBlocks;
//IShiftable properties
public CoordinateSystem HomeCoordinateSystem;
public CoordinateSystem CurrentCoordinateSystem;
//shifting methods from the IShiftableCollection Interface
public void ShiftGeometry(Shift shiftToApply, CoordinateSystem systemShiftIsBasedOn)
{
foreach (LegoBlock block in this.LegoBlocks)
{
block.Shift(shiftToApply, systemShiftIsBasedOn);
}
}
public void ShiftGeometryToCoordinateSystem(CoordinateSystem toShiftTo)
{
foreach (LegoBlock block in this.LegoBlocks)
{
block.ShiftToCoordinateSystem(toShiftTo);
}
}
//other methods
...
}