Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

node.h

00001 //
00002 // OpenVRML
00003 //
00004 // Copyright (C) 1998  Chris Morley
00005 // Copyright (C) 2002  Braden McDaniel
00006 //
00007 // This library is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 2.1 of the License, or (at your option) any later version.
00011 //
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015 // Lesser General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this library; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 # ifndef OPENVRML_NODE_H
00023 #   define OPENVRML_NODE_H
00024 
00025 #   include <iostream>
00026 #   include <list>
00027 #   include <set>
00028 #   include <stdexcept>
00029 #   include <utility>
00030 #   include "field.h"
00031 #   include "fieldvalueptr.h"
00032 #   include "nodetypeptr.h"
00033 #   include "scopeptr.h"
00034 #   include "Viewer.h"
00035 #   include "VrmlRenderContext.h"
00036 
00037 namespace OpenVRML {
00038 
00039     class OPENVRML_SCOPE NodeInterface {
00040     public:
00041         enum Type {
00042             invalidType,
00043             eventIn,
00044             eventOut,
00045             exposedField,
00046             field
00047         };
00048 
00049         Type type;
00050         FieldValue::Type fieldType;
00051         std::string id;
00052 
00053         NodeInterface(Type type,
00054                       FieldValue::Type fieldType,
00055                       const std::string & id);
00056     };
00057 
00058     std::ostream & OPENVRML_SCOPE operator<<(std::ostream & out,
00059                                              NodeInterface::Type type);
00060     std::istream & OPENVRML_SCOPE operator>>(std::istream & in,
00061                                              NodeInterface::Type & type);
00062 
00063     inline bool operator==(const NodeInterface & lhs, const NodeInterface & rhs)
00064         throw ()
00065     {
00066         return lhs.type == rhs.type
00067                 && lhs.fieldType == rhs.fieldType
00068                 && lhs.id == rhs.id;
00069     }
00070 
00071     inline bool operator!=(const NodeInterface & lhs, const NodeInterface & rhs)
00072         throw ()
00073     {
00074         return !(lhs == rhs);
00075     }
00076 
00077     class NodeType;
00078 
00079     class OPENVRML_SCOPE UnsupportedInterface : public std::runtime_error {
00080     public:
00081         explicit UnsupportedInterface(const std::string & message);
00082         UnsupportedInterface(const NodeType & nodeType,
00083                              const std::string & interfaceId);
00084         UnsupportedInterface(const NodeType & nodeType,
00085                              NodeInterface::Type interfaceType,
00086                              const std::string & interfaceId);
00087         virtual ~UnsupportedInterface() throw ();
00088     };
00089 
00090 
00091     class OPENVRML_SCOPE NodeInterfaceSet {
00092         struct IdLess :
00093                 std::binary_function<NodeInterface, NodeInterface, bool> {
00094             bool operator()(const NodeInterface & lhs,
00095                             const NodeInterface & rhs) const
00096             {
00097                 return lhs.id < rhs.id;
00098             }
00099         };
00100 
00101         std::set<NodeInterface, IdLess> nodeInterfaceSet;
00102 
00103     public:
00104         typedef std::set<NodeInterface, IdLess>::const_iterator const_iterator;
00105 
00106         void add(const NodeInterface & nodeInterface)
00107                 throw (std::invalid_argument, std::bad_alloc);
00108         const_iterator begin() const throw ();
00109         const_iterator end() const throw ();
00110         const_iterator findInterface(const std::string & id) const throw ();
00111     };
00112 
00113     inline NodeInterfaceSet::const_iterator NodeInterfaceSet::begin() const
00114         throw ()
00115     {
00116         return this->nodeInterfaceSet.begin();
00117     }
00118 
00119     inline NodeInterfaceSet::const_iterator NodeInterfaceSet::end() const
00120         throw ()
00121     {
00122         return this->nodeInterfaceSet.end();
00123     }
00124 
00125 
00126     class Browser;
00127     class Viewer;
00128 
00129     class OPENVRML_SCOPE NodeClass {
00130     public:
00131         Browser & browser;
00132 
00133         virtual ~NodeClass() throw () = 0;
00134         virtual void initialize(double time) throw ();
00135         virtual void render(Viewer & viewer) throw ();
00136         virtual const NodeTypePtr createType(const std::string & id,
00137                                              const NodeInterfaceSet & interfaces)
00138             throw (UnsupportedInterface, std::bad_alloc) = 0;
00139 
00140     protected:
00141         explicit NodeClass(Browser & browser) throw ();
00142     };
00143 
00144     class OPENVRML_SCOPE NodeType {
00145     public:
00146         NodeClass & nodeClass;
00147         const std::string id;
00148 
00149         virtual ~NodeType() throw () = 0;
00150 
00151         FieldValue::Type hasEventIn(const std::string & id) const throw ();
00152         FieldValue::Type hasEventOut(const std::string & id) const throw ();
00153         FieldValue::Type hasField(const std::string & id) const throw ();
00154         FieldValue::Type hasExposedField(const std::string & id) const throw ();
00155 
00156         virtual const NodeInterfaceSet & getInterfaces() const throw () = 0;
00157         virtual const NodePtr createNode(const ScopePtr & scope) const
00158             throw (std::bad_alloc) = 0;
00159 
00160     protected:
00161         NodeType(NodeClass & nodeClass, const std::string & id)
00162             throw (std::bad_alloc);
00163     };
00164 
00165 
00166     class OPENVRML_SCOPE FieldValueTypeMismatch : public std::runtime_error {
00167     public:
00168         FieldValueTypeMismatch();
00169         virtual ~FieldValueTypeMismatch() throw ();
00170     };
00171 
00172 
00173     class Scope;
00174     class VrmlMatrix;
00175     class NodeVisitor;
00176     class BVolume;
00177     class ScriptNode;
00178     class AppearanceNode;
00179     class ChildNode;
00180     class ColorNode;
00181     class CoordinateNode;
00182     class FontStyleNode;
00183     class GeometryNode;
00184     class MaterialNode;
00185     class NormalNode;
00186     class SoundSourceNode;
00187     class TextureNode;
00188     class TextureCoordinateNode;
00189     class TextureTransformNode;
00190     class FontFace;
00191 
00192     namespace Vrml97Node {
00193         class Anchor;
00194         class AudioClip;
00195         class CylinderSensor;
00196         class Group;
00197         class AbstractLight;
00198         class MovieTexture;
00199         class NavigationInfo;
00200         class PlaneSensor;
00201         class PointLight;
00202         class SphereSensor;
00203         class SpotLight;
00204         class TimeSensor;
00205         class TouchSensor;
00206         class Viewpoint;
00207     }
00208 
00209     class Scene;
00210 
00211     std::ostream & operator<<(std::ostream & out, const Node & node);
00212 
00213     class OPENVRML_SCOPE Node {
00214         friend std::ostream & operator<<(std::ostream & out, const Node & node);
00215 
00216     public:
00217         struct Route {
00218             const std::string fromEventOut;
00219             const NodePtr toNode;
00220             const std::string toEventIn;
00221 
00222             Route(const std::string & fromEventOut, const NodePtr & toNode,
00223                   const std::string & toEventIn);
00224             Route(const Route & route);
00225         };
00226 
00227         typedef std::list<Route> RouteList;
00228 
00229         struct PolledEventOutValue {
00230             const FieldValuePtr value;
00231             bool modified;
00232 
00233             PolledEventOutValue();
00234             PolledEventOutValue(const FieldValuePtr & value, bool modified);
00235         };
00236 
00237     private:
00238         ScopePtr scope;
00239         Scene * scene;
00240         RouteList routes;
00241 
00242         typedef std::map<std::string, PolledEventOutValue *> EventOutISMap;
00243         EventOutISMap eventOutISMap;
00244 
00245     public:
00246         const NodeType & nodeType;
00247 
00248         virtual ~Node() throw () = 0;
00249 
00250         const std::string getId() const;
00251         void setId(const std::string & nodeId);
00252 
00253         const ScopePtr & getScope() const throw ();
00254 
00255         Scene * getScene() const throw ();
00256 
00257         std::ostream & print(std::ostream & out, size_t indent) const;
00258 
00259         bool accept(NodeVisitor & visitor);
00260         void resetVisitedFlag() throw ();
00261 
00262         void addEventOutIS(const std::string & eventOut,
00263                            PolledEventOutValue * eventOutValue)
00264             throw (UnsupportedInterface, std::bad_alloc);
00265 
00266         void initialize(Scene & scene, double timestamp) throw (std::bad_alloc);
00267 
00268         void setField(const std::string & id, const FieldValue & value)
00269             throw (UnsupportedInterface, std::bad_cast, std::bad_alloc);
00270         const FieldValue & getField(const std::string & id) const
00271             throw (UnsupportedInterface);
00272         void processEvent(const std::string & id, const FieldValue & value,
00273                           double timestamp)
00274             throw (UnsupportedInterface, std::bad_cast, std::bad_alloc);
00275         const FieldValue & getEventOut(const std::string & id) const
00276             throw (UnsupportedInterface);
00277 
00278         virtual const ScriptNode * toScript() const throw ();
00279         virtual ScriptNode * toScript() throw ();
00280         virtual const AppearanceNode * toAppearance() const throw ();
00281         virtual AppearanceNode * toAppearance() throw ();
00282         virtual const ChildNode * toChild() const throw ();
00283         virtual ChildNode * toChild() throw ();
00284         virtual const ColorNode * toColor() const throw ();
00285         virtual ColorNode * toColor() throw ();
00286         virtual const CoordinateNode * toCoordinate() const throw ();
00287         virtual CoordinateNode * toCoordinate() throw ();
00288         virtual const FontStyleNode * toFontStyle() const throw ();
00289         virtual FontStyleNode * toFontStyle() throw () ;
00290         virtual const GeometryNode * toGeometry() const throw ();
00291         virtual GeometryNode * toGeometry() throw ();
00292         virtual const MaterialNode * toMaterial() const throw ();
00293         virtual MaterialNode * toMaterial() throw ();
00294         virtual const NormalNode * toNormal() const throw ();
00295         virtual NormalNode * toNormal() throw ();
00296         virtual const SoundSourceNode * toSoundSource() const throw ();
00297         virtual SoundSourceNode * toSoundSource() throw ();
00298         virtual const TextureNode * toTexture() const throw ();
00299         virtual TextureNode * toTexture() throw ();
00300         virtual const TextureCoordinateNode * toTextureCoordinate() const
00301             throw ();
00302         virtual TextureCoordinateNode * toTextureCoordinate() throw ();
00303         virtual const TextureTransformNode * toTextureTransform() const
00304             throw ();
00305         virtual TextureTransformNode * toTextureTransform() throw ();
00306 
00307         virtual Vrml97Node::Anchor * toAnchor() const;
00308         virtual Vrml97Node::AudioClip * toAudioClip() const;
00309         virtual Vrml97Node::CylinderSensor * toCylinderSensor() const;
00310         virtual Vrml97Node::Group * toGroup() const;
00311         virtual Vrml97Node::AbstractLight * toLight() const;
00312         virtual Vrml97Node::MovieTexture * toMovieTexture() const;
00313         virtual Vrml97Node::NavigationInfo * toNavigationInfo() const;
00314         virtual Vrml97Node::PlaneSensor * toPlaneSensor() const;
00315         virtual Vrml97Node::PointLight * toPointLight() const;
00316         virtual Vrml97Node::SphereSensor * toSphereSensor() const;
00317         virtual Vrml97Node::SpotLight * toSpotLight() const;
00318         virtual Vrml97Node::TimeSensor * toTimeSensor() const;
00319         virtual Vrml97Node::TouchSensor * toTouchSensor() const;
00320         virtual Vrml97Node::Viewpoint * toViewpoint() const;
00321 
00322         // Indicate that the node state has changed, need to re-render
00323         void setModified();
00324         void clearModified() { d_modified = false; }
00325         virtual bool isModified() const;
00326         typedef std::list< Node* > NodePath;
00327 
00328 
00329         static void markPathModified(NodePath& path, bool mod, int flags = 0x003);
00330 
00331         // do the work of updatemodified. move this to be protected
00332         //
00333         virtual void updateModified(NodePath& path, int flags = 0x003);
00334 
00335         void updateModified(int flags = 0x003);
00336 
00337         virtual const BVolume * getBVolume() const;
00338 
00339         virtual void setBVolume(const BVolume & v);
00340 
00341         virtual void setBVolumeDirty(bool f);
00342 
00343         virtual bool isBVolumeDirty() const;
00344 
00345         // Add a ROUTE from a field in this node
00346         void addRoute(const std::string & fromEventOut,
00347                       const NodePtr & toNode, const std::string & toEventIn)
00348             throw (UnsupportedInterface, FieldValueTypeMismatch);
00349 
00350         // Delete a ROUTE from a field in this node
00351         void deleteRoute(const std::string & fromEventOut,
00352                          const NodePtr & toNode, const std::string & toEventIn)
00353             throw ();
00354 
00355         const RouteList & getRoutes() const;
00356 
00357         virtual void render(Viewer & viewer, VrmlRenderContext context);
00358 
00359 
00360         virtual void accumulateTransform(Node*);
00361 
00362         virtual Node* getParentTransform();
00363         virtual void inverseTransform(VrmlMatrix &);
00364 
00365     protected:
00366         Node(const NodeType & nodeType, const ScopePtr & scope);
00367 
00368         // Send a named event from this node.
00369         void emitEvent(const std::string & id, const FieldValue & fieldValue,
00370                        double timestamp)
00371             throw (std::bad_cast, std::bad_alloc);
00372 
00373         // True if a field changed since last render
00374         bool d_modified;
00375         bool d_bvol_dirty;
00376         bool visited;
00377 
00378     private:
00379         // Not copyable.
00380         Node(const Node &);
00381         Node & operator=(const Node &);
00382 
00383         virtual void initializeImpl(double timestamp) throw (std::bad_alloc);
00384 
00385         virtual void setFieldImpl(const std::string & id,
00386                                   const FieldValue & value)
00387             throw (UnsupportedInterface, std::bad_cast, std::bad_alloc) = 0;
00388 
00389         virtual const FieldValue & getFieldImpl(const std::string & id) const
00390             throw (UnsupportedInterface) = 0;
00391 
00392         virtual void processEventImpl(const std::string & id,
00393                                       const FieldValue & value,
00394                                       double timestamp)
00395             throw (UnsupportedInterface, std::bad_cast, std::bad_alloc) = 0;
00396 
00397         virtual const FieldValue & getEventOutImpl(const std::string & id) const
00398             throw (UnsupportedInterface) = 0;
00399     };
00400 
00401     inline const ScopePtr & Node::getScope() const throw ()
00402     {
00403         return this->scope;
00404     }
00405 
00406     inline Scene * Node::getScene() const throw ()
00407     {
00408         return this->scene;
00409     }
00410 
00411     inline bool operator==(const Node::Route & lhs, const Node::Route & rhs)
00412         throw ()
00413     {
00414         return lhs.fromEventOut == rhs.fromEventOut
00415             && lhs.toNode == rhs.toNode
00416             && lhs.toEventIn == rhs.toEventIn;
00417     }
00418 
00419     inline bool operator!=(const Node::Route & lhs, const Node::Route & rhs)
00420         throw ()
00421     {
00422         return !(lhs == rhs);
00423     }
00424 
00425 
00426     class OPENVRML_SCOPE AppearanceNode : public virtual Node {
00427     public:
00428         virtual ~AppearanceNode() throw () = 0;
00429 
00430         virtual const AppearanceNode * toAppearance() const throw ();
00431         virtual AppearanceNode * toAppearance() throw ();
00432 
00433         virtual const SFNode & getMaterial() const throw () = 0;
00434         virtual const SFNode & getTexture() const throw () = 0;
00435         virtual const SFNode & getTextureTransform() const throw () = 0;
00436 
00437     protected:
00438         AppearanceNode(const NodeType & nodeType, const ScopePtr & scope);
00439     };
00440 
00441 
00442     class OPENVRML_SCOPE ChildNode : public virtual Node {
00443     public:
00444         virtual ~ChildNode() throw () = 0;
00445 
00446         virtual const ChildNode * toChild() const throw ();
00447         virtual ChildNode * toChild() throw ();
00448 
00449     protected:
00450         ChildNode(const NodeType & nodeType, const ScopePtr & scope);
00451     };
00452 
00453 
00454     class OPENVRML_SCOPE ColorNode : public virtual Node {
00455     public:
00456         virtual ~ColorNode() throw () = 0;
00457 
00458         virtual const ColorNode * toColor() const throw ();
00459         virtual ColorNode * toColor() throw ();
00460 
00461         virtual const MFColor & getColor() const throw () = 0;
00462 
00463     protected:
00464         ColorNode(const NodeType & nodeType, const ScopePtr & scope);
00465     };
00466 
00467 
00468     class OPENVRML_SCOPE CoordinateNode : public virtual Node {
00469     public:
00470         virtual ~CoordinateNode() throw () = 0;
00471 
00472         virtual const CoordinateNode * toCoordinate() const throw ();
00473         virtual CoordinateNode * toCoordinate() throw ();
00474 
00475         virtual const MFVec3f & getPoint() const throw () = 0;
00476 
00477     protected:
00478         CoordinateNode(const NodeType & nodeType, const ScopePtr & scope);
00479     };
00480 
00481 
00482     class OPENVRML_SCOPE FontStyleNode : public virtual Node {
00483     public:
00484         virtual ~FontStyleNode() throw () = 0;
00485 
00486         virtual const FontStyleNode * toFontStyle() const throw ();
00487         virtual FontStyleNode * toFontStyle() throw ();
00488 
00489         virtual const MFString & getFamily() const throw () = 0;
00490         virtual const SFBool & getHorizontal() const throw () = 0;
00491         virtual const MFString & getJustify() const throw () = 0;
00492         virtual const SFString & getLanguage() const throw () = 0;
00493         virtual const SFBool & getLeftToRight() const throw () = 0;
00494         virtual const SFFloat & getSize() const throw () = 0;
00495         virtual const SFFloat & getSpacing() const throw () = 0;
00496         virtual const SFString & getStyle() const throw () = 0;
00497         virtual const SFBool & getTopToBottom() const throw () = 0;
00498 
00499     protected:
00500         FontStyleNode(const NodeType & nodeType, const ScopePtr & scope);
00501     };
00502 
00503 
00504     class OPENVRML_SCOPE GeometryNode : public virtual Node {
00505     public:
00506         virtual ~GeometryNode() throw () = 0;
00507 
00508         virtual const GeometryNode * toGeometry() const throw ();
00509         virtual GeometryNode * toGeometry() throw ();
00510 
00511         virtual Viewer::Object insertGeometry(Viewer & viewer,
00512                                               VrmlRenderContext context) = 0;
00513         virtual const ColorNode * getColor() const throw ();
00514 
00515     protected:
00516         GeometryNode(const NodeType & nodeType, const ScopePtr & scope);
00517     };
00518 
00519 
00520     class OPENVRML_SCOPE MaterialNode : public virtual Node {
00521     public:
00522         virtual ~MaterialNode() throw () = 0;
00523 
00524         virtual const MaterialNode * toMaterial() const throw ();
00525         virtual MaterialNode * toMaterial() throw ();
00526 
00527         virtual const SFFloat & getAmbientIntensity() const throw () = 0;
00528         virtual const SFColor & getDiffuseColor() const throw () = 0;
00529         virtual const SFColor & getEmissiveColor() const throw () = 0;
00530         virtual const SFFloat & getShininess() const throw () = 0;
00531         virtual const SFColor & getSpecularColor() const throw () = 0;
00532         virtual const SFFloat & getTransparency() const throw () = 0;
00533 
00534     protected:
00535         MaterialNode(const NodeType & nodeType, const ScopePtr & scope);
00536     };
00537 
00538 
00539     class OPENVRML_SCOPE NormalNode : public virtual Node {
00540     public:
00541         virtual ~NormalNode() throw () = 0;
00542 
00543         virtual const NormalNode * toNormal() const throw ();
00544         virtual NormalNode * toNormal() throw ();
00545 
00546         virtual const MFVec3f & getVector() const throw () = 0;
00547 
00548     protected:
00549         NormalNode(const NodeType & nodeType, const ScopePtr & scope);
00550     };
00551 
00552 
00553     class OPENVRML_SCOPE SoundSourceNode : public virtual Node {
00554     public:
00555         virtual ~SoundSourceNode() throw () = 0;
00556         virtual const SoundSourceNode * toSoundSource() const throw ();
00557         virtual SoundSourceNode * toSoundSource() throw ();
00558 
00559     protected:
00560         SoundSourceNode(const NodeType & nodeType, const ScopePtr & scope);
00561     };
00562 
00563 
00564     class OPENVRML_SCOPE TextureNode : public virtual Node {
00565     public:
00566         virtual ~TextureNode() throw () = 0;
00567 
00568         virtual const TextureNode * toTexture() const throw ();
00569         virtual TextureNode * toTexture() throw ();
00570 
00571         virtual size_t nComponents() const throw () = 0;
00572         virtual size_t width() const throw () = 0;
00573         virtual size_t height() const throw () = 0;
00574         virtual size_t nFrames() const throw () = 0;
00575         virtual const unsigned char * pixels() const throw () = 0;
00576         virtual const SFBool & getRepeatS() const throw () = 0;
00577         virtual const SFBool & getRepeatT() const throw () = 0;
00578 
00579     protected:
00580         TextureNode(const NodeType & nodeType, const ScopePtr & scope);
00581     };
00582 
00583 
00584     class OPENVRML_SCOPE TextureCoordinateNode : public virtual Node {
00585     public:
00586         virtual ~TextureCoordinateNode() throw () = 0;
00587 
00588         virtual const TextureCoordinateNode * toTextureCoordinate() const
00589             throw ();
00590         virtual TextureCoordinateNode * toTextureCoordinate() throw ();
00591 
00592         virtual const MFVec2f & getPoint() const throw () = 0;
00593 
00594     protected:
00595         TextureCoordinateNode(const NodeType & nodeType,
00596                               const ScopePtr & scope);
00597     };
00598 
00599 
00600     class OPENVRML_SCOPE TextureTransformNode : public virtual Node {
00601     public:
00602         virtual ~TextureTransformNode() throw () = 0;
00603 
00604         virtual const TextureTransformNode * toTextureTransform() const throw ();
00605         virtual TextureTransformNode * toTextureTransform() throw ();
00606 
00607     protected:
00608         TextureTransformNode(const NodeType & nodeType, const ScopePtr & scope);
00609     };
00610 
00611 
00612     class OPENVRML_SCOPE NodeVisitor {
00613     public:
00614         virtual ~NodeVisitor() = 0;
00615 
00616         virtual void visit(Node & node) = 0;
00617     };
00618 }
00619 
00620 # endif