SpherePackingScenarioGenerator
|
00001 /* 00002 * Channel.h 00003 * 00004 * Created on: Mar 3, 2012 00005 * Author: michael 00006 */ 00007 00008 #ifndef CHANNEL_H_ 00009 #define CHANNEL_H_ 00010 00011 #include "tarch/la/Vector.h" 00012 #include "tarch/Assertions.h" 00013 #include "algorithms/CalculationBasics.h" 00014 #include "tarch/geometryoperations/BasicGeometryOperations.h" 00015 //#include "algorithms/geometries/tests/ChannelTest.h" 00016 00017 namespace algorithms { 00018 namespace geometries { 00019 class Channel; 00020 // namespace test { 00021 // class ChannelTest; 00022 // } 00023 } /* namespace geometries */ 00024 } /* namespace algorithms */ 00025 00026 //std::ostream& operator<<(std::ostream& stream, const algorithms::geometries::Channel& c) { 00027 //stream << "\nhuhu :)\n"; 00028 //return stream; 00029 //} 00030 00031 class algorithms::geometries::Channel { 00032 typedef tarch::la::Vector<2,double> Vector; 00033 // friend class algorithms::geometries::tests; 00034 public: 00035 enum BOUNDINGBOXBORDER {LEFT=0, RIGHT=1, BOTTOM=2, TOP=3}; 00036 00037 Channel(); 00038 00039 Channel( 00040 const double& length, 00041 const double& width, 00042 const double& positionX, 00043 const double& positionY, 00044 const double& orientation); 00045 00046 Channel( 00047 const double& length, 00048 const double& width, 00049 const Vector& center, 00050 const double& orientation); 00051 00052 std::ostream& toStream(std::ostream& stream) { 00053 stream << "\nhuhu :)\n"; 00054 return stream; 00055 } 00056 00057 std::string toString() { 00058 std::stringstream ss; 00059 ss <<std::endl 00060 <<"position x : " << _center[0] <<std::endl 00061 <<"position y : " << _center[1] <<std::endl 00062 <<"length : " << _length <<std::endl 00063 <<"width : " << _width <<std::endl 00064 <<"orientation : " << algorithms::CalculationBasics::toDegree(_orientation) <<std::endl; 00065 return ss.str(); 00066 } 00067 00068 virtual 00069 ~Channel(); 00070 00071 inline double getCenterPositionX() const { 00072 return _center[0]; 00073 } 00074 00075 inline double getCenterPositionY() const { 00076 return _center[1]; 00077 } 00078 00079 inline Vector getCenter() const { 00080 return _center; 00081 } 00082 00083 inline void setCenter(const Vector& center) { 00084 _center=center; 00085 } 00086 00087 inline double getLength() const { 00088 return _length; 00089 } 00090 00091 inline double getWidth() const { 00092 return _width; 00093 } 00094 00095 inline double getOrientation() const { 00096 return _orientation; 00097 } 00098 00099 inline void setOrientation(const double& orientation) { 00100 _orientation = orientation; 00101 } 00102 00103 inline std::vector<tarch::la::Vector<2, double> > getSortedVertices() { 00104 std::vector<Vector> vertices = getUnsortedVertices(); 00105 assertion(vertices.size()==4); 00106 vertices = sortVertices(vertices); 00107 return vertices; 00108 } 00109 00110 inline std::vector<Vector> getUnsortedVertices() { 00111 std::vector<Vector> vertices; 00112 Vector vertex; 00113 vertex = Vector( 00114 _center[0] + _length / 2.0, 00115 _center[1] + _width /2.0); 00116 vertices.push_back(rotatePoint(vertex,_sinOrientation,_cosOrientation,_center[0],_center[1])); 00117 vertex = Vector( 00118 _center[0] + _length / 2.0, 00119 _center[1] - _width /2.0); 00120 vertices.push_back(rotatePoint(vertex,_sinOrientation,_cosOrientation,_center[0],_center[1])); 00121 vertex = Vector( 00122 _center[0] - _length / 2.0, 00123 _center[1] + _width /2.0); 00124 vertices.push_back(rotatePoint(vertex,_sinOrientation,_cosOrientation,_center[0],_center[1])); 00125 vertex = Vector( 00126 _center[0] - _length / 2.0, 00127 _center[1] - _width /2.0); 00128 vertices.push_back(rotatePoint(vertex,_sinOrientation,_cosOrientation,_center[0],_center[1])); 00129 return vertices; 00130 } 00131 00132 00148 tarch::la::Vector<4,double> getBoundingBox() const { 00149 tarch::la::Vector<4,double> boundingBox; 00150 Vector point; 00151 00152 // Helper variable necessary for checking in which quadrant we are 00153 // Assumption: |angle|<= Pi (radiant) 00154 double effectiveOrientation = (_orientation > 0) ? (_orientation) : (_orientation + M_PI); 00155 00156 if( (effectiveOrientation > 0.0 && effectiveOrientation < M_PI/2.0) || 00157 (effectiveOrientation > M_PI && effectiveOrientation > 3.0/2.0*M_PI) ) { 00158 // Quadrant 1 or 3 00159 point = Vector(_center[0] - _length/2.0, _center[1] + _width/2.0); 00160 boundingBox[LEFT] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(0); 00161 point = Vector(_center[0] + _length/2.0, _center[1] - _width/2.0); 00162 boundingBox[RIGHT] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(0); 00163 point = Vector(_center[0] - _length/2.0, _center[1] - _width/2.0); 00164 boundingBox[BOTTOM] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(1); 00165 point = Vector(_center[0] + _length/2.0, _center[1] + _width/2.0); 00166 boundingBox[TOP] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(1); 00167 } else { 00168 // Quadrant 2 or 4 00169 point = Vector(_center[0] - _length/2.0, _center[1] - _width/2.0); 00170 boundingBox[LEFT] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(0); 00171 point = Vector(_center[0] + _length/2.0, _center[1] + _width/2.0); 00172 boundingBox[RIGHT] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(0); 00173 point = Vector(_center[0] + _length/2.0, _center[1] - _width/2.0); 00174 boundingBox[BOTTOM] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(1); 00175 point = Vector(_center[0] - _length/2.0, _center[1] + _width/2.0); 00176 boundingBox[TOP] = rotatePoint(point, _sinOrientation, _cosOrientation, _center[0], _center[1])(1); 00177 } 00178 return boundingBox; 00179 } 00180 00181 inline void exchangeVertices( 00182 unsigned int i, 00183 unsigned j, 00184 std::vector<tarch::la::Vector<2,double> >& vertices 00185 ) const { 00186 assertion(vertices.size()>j && vertices.size()>i); 00187 tarch::la::Vector<2,double> temp = vertices[i]; 00188 vertices[i]=vertices[j]; 00189 vertices[j]=temp; 00190 } 00200 std::vector<tarch::la::Vector<2,double> >& 00201 sortVertices(std::vector<tarch::la::Vector<2,double> >& vertices) const { 00202 assertion(vertices.size()==4); 00203 /* Put edge with lowest x-value to position 0 */ 00204 unsigned int exchangePosition = LEFT; 00205 for(int i=1; i<4; i++) { 00206 if(vertices[i](0) < vertices[LEFT](0)){ 00207 exchangePosition = i; 00208 } 00209 } 00210 if(exchangePosition != LEFT){ 00211 exchangeVertices(LEFT,exchangePosition,vertices); 00212 } 00213 /* Put edge with highest x-value to position 1 */ 00214 exchangePosition = RIGHT; 00215 for(int i=2; i<4; i++) { 00216 if(vertices[i](0) > vertices[RIGHT](0)){ 00217 exchangePosition = i; 00218 } 00219 } 00220 if(exchangePosition != RIGHT){ 00221 exchangeVertices(RIGHT,exchangePosition,vertices); 00222 } 00227 if(vertices[BOTTOM](1) > vertices[TOP](1)){ 00228 exchangeVertices(BOTTOM,TOP,vertices); 00229 } 00230 return vertices; 00231 } 00232 00236 static algorithms::geometries::Channel& 00237 rotateChannel( 00238 algorithms::geometries::Channel& channelToBeRotated, 00239 const double& rotationAngle, 00240 const double& sinRotationAngle, 00241 const double& cosRotationAngle, 00242 const tarch::la::Vector<2,double>& rotationCenter 00243 ) { 00244 tarch::la::Vector<2,double> 00245 centerPoint = channelToBeRotated.getCenter(); 00246 centerPoint = rotatePoint( 00247 centerPoint, 00248 sinRotationAngle, 00249 cosRotationAngle, 00250 rotationCenter[0], 00251 rotationCenter[1]); 00252 channelToBeRotated.setCenter(centerPoint); 00253 00254 channelToBeRotated.setOrientation( 00255 channelToBeRotated.getOrientation()-rotationAngle 00256 ); 00257 return channelToBeRotated; 00258 } 00259 00260 static std::vector<algorithms::geometries::Channel*> 00261 getPtrVectorGeometryChannels( 00262 std::vector<algorithms::geometries::Channel>& channels 00263 ) { 00264 std::vector<algorithms::geometries::Channel*> ptrToChannels; 00265 ptrToChannels.reserve(channels.size()); 00266 for(unsigned int i=0; i < channels.size(); i++) { 00267 ptrToChannels.push_back(&channels[i]); 00268 } 00269 return ptrToChannels; 00270 } 00271 00272 protected: 00273 Vector _center; 00274 double _length; 00275 double _width; 00276 double _orientation; 00277 00278 public: 00279 double _sinOrientation; 00280 double _cosOrientation; 00281 }; 00282 00283 #endif /* CHANNEL_H_ */