FragmentSkeleton.hpp

00001 #ifndef __FRAGMENT_SKELETON_HPP
00002 #define __FRAGMENT_SKELETON_HPP
00003 
00004 #include <grid/Grid.hpp>
00005 #include <grid/CartesianChart.hpp>
00006 #include <eagle/PhysicalSpace.hpp>
00007 #include <aerie/BoundingBox.hpp>
00008 
00009 #include "gridopDllApi.h"
00010 
00011 namespace Fiber
00012 {
00013 
00019 struct gridop_API FragmentSkeleton
00020 {
00021         typedef MemArray<1, std::string>        FragmentNamesMemArray_t;
00022         typedef MultiArray<1, std::string>      FragmentNamesArray_t;
00023 
00024         struct  gridop_API exception : ::std::exception
00025         {
00026                 const char*what() const throw()
00027                 {       return "FragmentSkeleton Exception";            }
00028         };
00029 
00030         struct  gridop_API NoVertices : exception
00031         {
00032                 const char*what() const throw()
00033                 {       return "FragmentSkeleton: No Vertices in Grid object";          }
00034         };
00035 
00036 static  const char*MinFieldName();
00037 static  const char*MaxFieldName();
00038 
00039 static  SkeletonID ID(int VertexDims)
00040         {
00041                 return SkeletonID(VertexDims, 2);
00042         } 
00043 
00050 static  SkeletonID ID(const Grid&G)
00051         {
00052         RefPtr<Skeleton> Vertices = G.findVertices(); 
00053                 if (!Vertices)
00054                         throw NoVertices();
00055 
00056         int     VertexDims = Vertices->Dims();
00057                 return SkeletonID(VertexDims, 2);
00058         }
00059 
00060 
00061 static  RefPtr<Skeleton> getFragments(const Grid&G)
00062         {
00063         RefPtr<Skeleton> Vertices = G.findVertices(); 
00064                 if (!Vertices)
00065                         return NullPtr();
00066 
00067         int     VertexDims = Vertices->Dims(); 
00068                 return G( SkeletonID(VertexDims, 2) );
00069         }
00070 
00071 static  Skeleton& makeFragments(Grid&G)
00072         {
00073                 return G[ ID(G) ];
00074         }
00075 
00076 
00077 
00078 static  RefPtr<Representation> getFragmentsAsVertices(const Grid&G)
00079         {
00080         RefPtr<Skeleton> Vertices = G.findVertices(); 
00081                 if (!Vertices)  return NullPtr();
00082 
00083         RefPtr<Skeleton> Fragments = getFragments(G); 
00084                 if (!Fragments) return NullPtr();
00085 
00086                 return (*Fragments)( Vertices );
00087         }
00088 
00089 static  Representation&makeFragmentsAsVertices(Grid&G)
00090         {
00091         RefPtr<Skeleton> Vertices = G.findVertices(); 
00092                 if (!Vertices) throw NoVertices(); 
00093 
00094         Skeleton& Fragments = makeFragments(G);
00095 
00096                 return Fragments[ Vertices ];
00097         }
00098 
00099 static  RefPtr<Field> makeFragmentNames(Grid&G)
00100         {
00101                 return makeFragmentsAsVertices(G)[ FIBER_POSITIONS ]; 
00102         }
00103 
00104 static  Representation&makeFragmentCoordinates(Grid&G, const RefPtr<Chart>&C)
00105         {
00106         Skeleton& Fragments = makeFragments(G);
00107                 return Fragments[ C ];
00108         }
00109 
00110 static  RefPtr<Field> makeFragmentCoordinatesCenter(Grid&G, const RefPtr<Chart>&C)
00111         {
00112                 return makeFragmentCoordinates(G, C)[ FIBER_POSITIONS ];
00113         }
00114 
00115 static  RefPtr<Field> makeFragmentCoordinatesMin(Grid&G, const RefPtr<Chart>&C)
00116         {
00117                 return makeFragmentCoordinates(G, C)[ MaxFieldName() ];
00118         }
00119 
00120 static  RefPtr<Field> makeFragmentCoordinatesMax(Grid&G, const RefPtr<Chart>&C)
00121         {
00122                 return makeFragmentCoordinates(G, C)[ MaxFieldName() ];
00123         }
00124 
00125         RefPtr<Grid>        TheGrid;
00126 
00127         FragmentSkeleton(const RefPtr<Grid>&g)
00128         : TheGrid(g)
00129         {} 
00130 
00131         Skeleton& makeFragments() 
00132         {
00133                 assert(TheGrid);
00134                 return makeFragments(*TheGrid);
00135         }
00136 
00137         RefPtr<Field>   makeFragmentNames() const
00138         {
00139                 assert(TheGrid);
00140                 return makeFragmentNames(*TheGrid);
00141         } 
00142 
00143         std::string     getFragmentName(index_t i) const
00144         {
00145         RefPtr<Field> F = makeFragmentNames(); 
00146                 assert(F);
00147         RefPtr<FragmentNamesMemArray_t> A = F->getData(); 
00148                 assert(A);
00149                 return (*A)[ i ];
00150         }
00151 };
00152 
00153 template <class Coordinates = Eagle::PhysicalSpace::point>
00154 struct  FragmentCoordinates : public FragmentSkeleton
00155 {
00156         typedef MemArray<1, Coordinates>        PointMemArray_t;
00157         typedef MultiArray<1, Coordinates>      PointArray_t;
00158 
00159         FragmentCoordinates(const RefPtr<Grid>&g)
00160         : FragmentSkeleton(g)
00161         {}
00162 
00163 };
00164 
00165 typedef FragmentCoordinates<Eagle::PhysicalSpace::point> 
00166         CartesianFragmentCoordinates;
00167 
00168 struct  gridop_API CartesianFragments : public CartesianFragmentCoordinates
00169 {
00170         typedef CartesianFragmentCoordinates Base_t;
00171 
00172         typedef MemArray<1, RefPtr<Eagle::BoundingBox> >   BoundingBoxMemArray_t;
00173         typedef MultiArray<1, RefPtr<Eagle::BoundingBox> > BoundingBoxArray_t;
00174 
00175         CartesianFragments(const RefPtr<Grid>&g)
00176         : Base_t(g)
00177         {}
00178 
00179         bool    buildFragmentCoordinates();
00180 
00181         Representation&makeFragmentCoordinates()
00182         {
00183         RefPtr<Chart>  myCartesian = TheGrid->makeChart( typeid(Fiber::CartesianChart3D) ); 
00184                 return Base_t::makeFragmentCoordinates(*TheGrid, myCartesian);
00185         }
00186 
00187         std::pair<RefPtr<Field>, RefPtr<Field> >
00188                 makeFragmentCoordinatesMinMax()
00189         {
00190         Representation&R = makeFragmentCoordinates(); 
00191 
00192                 return std::pair<RefPtr<Field>, RefPtr<Field> >(
00193                         R[ MinFieldName() ], R[ MaxFieldName() ]
00194                         );
00195         }
00196 
00197         RefPtr<Eagle::BoundingBox> getBBox(index_t FragmentID); 
00198 
00199         RefPtr<FragmentID> getFragmentID(index_t I) const
00200         {
00201                 assert( TheGrid );
00202         RefPtr<Representation> CartesianRep = TheGrid->getCartesianRepresentation(); 
00203                 assert( CartesianRep ); 
00204                 assert( CartesianRep->getPositions() );
00205                 return CartesianRep->getPositions()->getFragmentID( getFragmentName(I) );
00206         }
00207 };
00208 
00209 } // namespace Fiber
00210 
00211 
00212 #endif //  __FRAGMENT_SKELETON_HPP
00213 
00214