00001 //----------------------------------------------------------------------------------- 00002 // 00003 // Pro-Vocation Light Engine (PVLE) 00004 // Copyright (C) 2007-2009 Sukender, KinoX & Buzib 00005 // For more information, contact us : sukender@free.fr 00006 // 00007 // This program is free software; you can redistribute it and/or modify 00008 // it under the terms of the GNU General Public License as published by 00009 // the Free Software Foundation; either version 3 of the License, or 00010 // (at your option) any later version. 00011 // 00012 // For any use that is not compatible with the terms of the GNU 00013 // General Public License, please contact the authors for alternative 00014 // licensing options. 00015 // 00016 // This program is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 // GNU General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU General Public License along 00022 // with this program; if not, write to the Free Software Foundation, Inc., 00023 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00024 // 00025 //----------------------------------------------------------------------------------- 00026 00027 #ifndef PHY_VISITOR_SMART_H 00028 #define PHY_VISITOR_SMART_H 00029 00030 //#include <PVLE/Export.h> 00031 #include <PVLE/Physics/Visitor.h> 00032 #include <set> 00033 00034 00035 namespace Physics { 00036 00038 class VisitorSmart : public Visitor { 00039 public: 00041 virtual void apply(Body & body) { 00042 if (!isVisited(body)) { 00043 applyOnce(body); 00044 visited(body); 00045 } 00046 } 00048 virtual void apply(Joint & joint) { 00049 if (!isVisited(joint)) { 00050 applyOnce(joint); 00051 visited(joint); 00052 } 00053 } 00054 00056 virtual void reset() { 00057 visitedBodies.clear(); 00058 visitedJoints.clear(); 00059 } 00060 00061 protected: 00062 virtual void applyOnce(Body & body) {} 00063 virtual void applyOnce(Joint & joint) {} 00064 00065 inline bool isVisited(Body & body) { return visitedBodies.find(&body) != visitedBodies.end(); } 00066 inline void visited(Body & body) { ASSERT(!isVisited(body)); visitedBodies.insert(&body); } 00067 inline bool isVisited(Joint & joint) { return visitedJoints.find(&joint) != visitedJoints.end(); } 00068 inline void visited(Joint & joint) { ASSERT(!isVisited(joint)); visitedJoints.insert(&joint); } 00069 std::set<Body *> visitedBodies; 00070 std::set<Joint *> visitedJoints; 00071 }; 00072 00073 } // namespace PHY_VISITOR_SMART_H 00074 00075 00076 #endif // PHY_VISITOR_H