Simbody
|
00001 #ifndef SimTK_SimTKCOMMON_DECORATIVE_GEOMETRY_H_ 00002 #define SimTK_SimTKCOMMON_DECORATIVE_GEOMETRY_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * SimTK Core: SimTKcommon * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK Core biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org. * 00011 * * 00012 * Portions copyright (c) 2005-7 Stanford University and the Authors. * 00013 * Authors: Michael Sherman * 00014 * Contributors: Jack Middleton * 00015 * * 00016 * Permission is hereby granted, free of charge, to any person obtaining a * 00017 * copy of this software and associated documentation files (the "Software"), * 00018 * to deal in the Software without restriction, including without limitation * 00019 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 00020 * and/or sell copies of the Software, and to permit persons to whom the * 00021 * Software is furnished to do so, subject to the following conditions: * 00022 * * 00023 * The above copyright notice and this permission notice shall be included in * 00024 * all copies or substantial portions of the Software. * 00025 * * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 00029 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 00030 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 00031 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 00032 * USE OR OTHER DEALINGS IN THE SOFTWARE. * 00033 * -------------------------------------------------------------------------- */ 00034 00067 /* TODO: NEW TEXT -- NOT IMPLEMENTED YET (just thinking out loud) 00068 * 00069 * Each kind of Decoration defines a parameterized object, whose appearance 00070 * is specified once its parameters are known. Parameters will be scalars, 00071 * points, direction vectors, and transforms. These involve dependencies on 00072 * a fixed set of reference frames: the ground body frame, the moving body frames, 00073 * and the screen or viewing frame. The body frames are defined by the System; 00074 * while the viewing frame is defined by whatever application is communicating 00075 * to a user. The World frame of the viewer and the Ground frame of the System 00076 * are the same. At run time a System can always report the configuration of its 00077 * moving bodies with respect to Ground, and hence these can be placed in the 00078 * scene with respect to the World frame. A text label, for example, could be tethered 00079 * to a point on a moving body while defined to remain parallel to the viewing plane. 00080 * 00081 * In addition to their geometric representations, Decorations defined here can 00082 * state preferences, if any, for a variety of viewing options such as color, 00083 * drawing style, and transparency. If left unspecified, the viewer will have to 00084 * determine how to set these options; if specified the viewer may want to use 00085 * the specified options but can override them if it has good reason to do so. 00086 * Many different implementations of Decorations are possible. The generic 00087 * implementation is defined below as an abstract class; each viewer will use 00088 * a particular implementation that defines a concrete implementation for each of the virtual 00089 * methods of the abstraction. 00090 * 00091 * There are two Stages associated with a Decoration. When is the Decoration 00092 * known to exist (including its parameterization), and when are values for its 00093 * parameters calculatable. For example, a permanent body-fixed sphere is known 00094 * to exist a the Topological stage (i.e., as soon as the body exists). Its 00095 * configuration in the World frame, however, cannot be computed until we 00096 * know the body's configuration, that is, Stage::Position. 00097 */ 00098 00099 #include "SimTKcommon/Simmatrix.h" 00100 #include "SimTKcommon/internal/PolygonalMesh.h" 00101 00102 #include <cassert> 00103 00104 00105 namespace SimTK { 00106 00107 class AnalyticGeometry; 00108 00109 00110 // Some common RGB values; 00111 static const Vec3 Black = Vec3( 0, 0, 0); 00112 static const Vec3 Gray = Vec3(.5,.5,.5); 00113 static const Vec3 Red = Vec3( 1, 0, 0); 00114 static const Vec3 Green = Vec3( 0, 1, 0); 00115 static const Vec3 Blue = Vec3( 0, 0, 1); 00116 static const Vec3 Yellow = Vec3( 1, 1, 0); 00117 static const Vec3 Orange = Vec3( 1,.5, 0); 00118 static const Vec3 Magenta = Vec3( 1, 0, 1); 00119 static const Vec3 Purple = Vec3(.5, 0,.5); 00120 static const Vec3 Cyan = Vec3( 0, 1, 1); 00121 static const Vec3 White = Vec3( 1, 1, 1); 00122 00123 // Drawing representations 00124 00125 class DecorativeGeometryImplementation; 00126 00132 class SimTK_SimTKCOMMON_EXPORT DecorativeGeometry { 00133 public: 00134 DecorativeGeometry() : rep(0) { } 00135 ~DecorativeGeometry(); 00136 DecorativeGeometry(const DecorativeGeometry&); 00137 DecorativeGeometry& operator=(const DecorativeGeometry&); 00138 00139 // Drawing modes. 00140 enum Representation { 00141 DrawPoints = 1, 00142 DrawWireframe = 2, 00143 DrawSurface = 3, 00144 00145 DrawDefault = -1 00146 }; 00147 00149 DecorativeGeometry(const AnalyticGeometry&); 00150 00155 DecorativeGeometry& setBodyId(int); 00156 00162 DecorativeGeometry& setTransform(const Transform& X_BG); 00163 00171 DecorativeGeometry& setResolution(Real); 00172 00180 DecorativeGeometry& setScale(Real); 00181 00184 int getBodyId() const; 00185 00188 Real getResolution() const; 00189 00193 const Transform& getTransform() const; 00194 00197 Real getScale() const; 00198 00202 DecorativeGeometry& setColor(const Vec3& rgb); // 0-1 for each color 00203 00207 DecorativeGeometry& setOpacity(Real); // 0-1; default is 1 (opaque) 00208 00213 DecorativeGeometry& setLineThickness(Real); 00214 00215 const Vec3& getColor() const; 00216 Real getOpacity() const; 00217 Real getLineThickness() const; 00218 00220 DecorativeGeometry& setFaceCamera(bool face); 00222 bool getFaceCamera() const; 00223 00226 DecorativeGeometry& setRepresentation(const Representation&); 00227 00229 Representation getRepresentation() const; 00230 00231 void implementGeometry(DecorativeGeometryImplementation&) const; 00232 00233 // Bookkeeping below here -- internal use only. Don't look below or you will 00234 // turn into a pillar of salt. 00235 bool isOwnerHandle() const; 00236 bool isEmptyHandle() const; 00237 explicit DecorativeGeometry(class DecorativeGeometryRep* r) : rep(r) { } 00238 bool hasRep() const {return rep!=0;} 00239 const DecorativeGeometryRep& getRep() const {assert(rep); return *rep;} 00240 DecorativeGeometryRep& updRep() {assert(rep); return *rep;} 00241 protected: 00242 DecorativeGeometryRep* rep; 00243 }; 00244 00256 class SimTK_SimTKCOMMON_EXPORT DecorativeLine : public DecorativeGeometry { 00257 public: 00258 explicit DecorativeLine(const Vec3& p1=Vec3(0), const Vec3& p2=Vec3(1)); // line between p1 and p2 00259 00260 // Retain the derived type when setting generic geometry options. 00261 DecorativeLine& setBodyId(int b) {DecorativeGeometry::setBodyId(b); return *this;} 00262 DecorativeLine& setTransform(const Transform& X_BD) {DecorativeGeometry::setTransform(X_BD); return *this;} 00263 DecorativeLine& setResolution(Real r) {DecorativeGeometry::setResolution(r); return *this;} 00264 DecorativeLine& setScale(Real s) {DecorativeGeometry::setScale(s); return *this;} 00265 DecorativeLine& setColor(const Vec3& rgb) {DecorativeGeometry::setColor(rgb); return *this;} 00266 DecorativeLine& setOpacity(Real o) {DecorativeGeometry::setOpacity(o); return *this;} 00267 DecorativeLine& setLineThickness(Real t) {DecorativeGeometry::setLineThickness(t); return *this;} 00268 DecorativeLine& setRepresentation(const Representation& r) { 00269 DecorativeGeometry::setRepresentation(r); return *this; 00270 } 00271 00272 // These are specific to lines. 00273 DecorativeLine& setPoint1(const Vec3& p1); 00274 DecorativeLine& setPoint2(const Vec3& p2); 00275 DecorativeLine& setEndpoints(const Vec3& p1, const Vec3& p2); 00276 00277 const Vec3& getPoint1() const; 00278 const Vec3& getPoint2() const; 00279 00280 SimTK_PIMPL_DOWNCAST(DecorativeLine, DecorativeGeometry); 00281 private: 00282 class DecorativeLineRep& updRep(); 00283 const DecorativeLineRep& getRep() const; 00284 }; 00285 00290 class SimTK_SimTKCOMMON_EXPORT DecorativeCircle : public DecorativeGeometry { 00291 public: 00292 explicit DecorativeCircle(Real radius=0.5); 00293 00294 void setRadius(Real); 00295 Real getRadius() const; 00296 00297 SimTK_PIMPL_DOWNCAST(DecorativeCircle, DecorativeGeometry); 00298 private: 00299 class DecorativeCircleRep& updRep(); 00300 const DecorativeCircleRep& getRep() const; 00301 }; 00302 00307 class SimTK_SimTKCOMMON_EXPORT DecorativeSphere : public DecorativeGeometry { 00308 public: 00309 explicit DecorativeSphere(Real radius=0.5); 00310 00311 void setRadius(Real); 00312 Real getRadius() const; 00313 00314 SimTK_PIMPL_DOWNCAST(DecorativeSphere, DecorativeGeometry); 00315 private: 00316 class DecorativeSphereRep& updRep(); 00317 const DecorativeSphereRep& getRep() const; 00318 }; 00319 00325 class SimTK_SimTKCOMMON_EXPORT DecorativeEllipsoid : public DecorativeGeometry { 00326 public: 00327 explicit DecorativeEllipsoid(const Vec3& radii = Vec3(0.5,1/3.,0.25)); 00328 00329 void setRadii(const Vec3&); 00330 const Vec3& getRadii() const; 00331 00332 SimTK_PIMPL_DOWNCAST(DecorativeEllipsoid, DecorativeGeometry); 00333 private: 00334 class DecorativeEllipsoidRep& updRep(); 00335 const DecorativeEllipsoidRep& getRep() const; 00336 }; 00337 00343 class SimTK_SimTKCOMMON_EXPORT DecorativeBrick : public DecorativeGeometry { 00344 public: 00345 explicit DecorativeBrick(const Vec3& halfLengths = Vec3(0.5)); 00346 00347 void setHalfLengths(const Vec3&); 00348 const Vec3& getHalfLengths() const; 00349 00350 SimTK_PIMPL_DOWNCAST(DecorativeBrick, DecorativeGeometry); 00351 private: 00352 class DecorativeBrickRep& updRep(); 00353 const DecorativeBrickRep& getRep() const; 00354 }; 00355 00361 class SimTK_SimTKCOMMON_EXPORT DecorativeCylinder : public DecorativeGeometry { 00362 public: 00363 explicit DecorativeCylinder(Real radius=0.5, Real halfHeight=0.5); 00364 00365 void setRadius(Real); 00366 void setHalfHeight(Real); 00367 Real getRadius() const; 00368 Real getHalfHeight() const; 00369 00370 SimTK_PIMPL_DOWNCAST(DecorativeCylinder, DecorativeGeometry); 00371 private: 00372 class DecorativeCylinderRep& updRep(); 00373 const DecorativeCylinderRep& getRep() const; 00374 }; 00375 00381 class SimTK_SimTKCOMMON_EXPORT DecorativeFrame : public DecorativeGeometry { 00382 public: 00383 explicit DecorativeFrame(Real axisLength=1); 00384 00385 void setAxisLength(Real); 00386 Real getAxisLength() const; 00387 00388 SimTK_PIMPL_DOWNCAST(DecorativeFrame, DecorativeGeometry); 00389 private: 00390 class DecorativeFrameRep& updRep(); 00391 const DecorativeFrameRep& getRep() const; 00392 }; 00393 00398 class SimTK_SimTKCOMMON_EXPORT DecorativeText : public DecorativeGeometry { 00399 public: 00400 explicit DecorativeText(const std::string& label=""); 00401 00402 void setText(const std::string& label); 00403 const std::string& getText() const; 00404 00405 SimTK_PIMPL_DOWNCAST(DecorativeText, DecorativeGeometry); 00406 private: 00407 class DecorativeTextRep& updRep(); 00408 const DecorativeTextRep& getRep() const; 00409 }; 00410 00414 class SimTK_SimTKCOMMON_EXPORT DecorativeMesh : public DecorativeGeometry { 00415 public: 00416 explicit DecorativeMesh(const PolygonalMesh& mesh); 00417 const PolygonalMesh& getMesh() const; 00418 00419 SimTK_PIMPL_DOWNCAST(DecorativeMesh, DecorativeGeometry); 00420 private: 00421 class DecorativeMeshRep& updRep(); 00422 const DecorativeMeshRep& getRep() const; 00423 }; 00424 00429 class SimTK_SimTKCOMMON_EXPORT DecorativeGeometryImplementation { 00430 public: 00431 virtual ~DecorativeGeometryImplementation() { } 00432 virtual void implementLineGeometry( const DecorativeLine&) = 0; 00433 virtual void implementBrickGeometry( const DecorativeBrick&) = 0; 00434 virtual void implementCylinderGeometry( const DecorativeCylinder&) = 0; 00435 virtual void implementCircleGeometry( const DecorativeCircle&) = 0; 00436 virtual void implementSphereGeometry( const DecorativeSphere&) = 0; 00437 virtual void implementEllipsoidGeometry(const DecorativeEllipsoid&)= 0; 00438 virtual void implementFrameGeometry( const DecorativeFrame&) = 0; 00439 virtual void implementTextGeometry( const DecorativeText&) = 0; 00440 virtual void implementMeshGeometry( const DecorativeMesh&) = 0; 00441 }; 00442 00443 } // namespace SimTK 00444 00445 #endif // SimTK_SimTKCOMMON_DECORATIVE_GEOMETRY_H_