1 | /*
|
---|
2 | * BaseShapes_impl.cpp
|
---|
3 | *
|
---|
4 | * Created on: Jun 18, 2010
|
---|
5 | * Author: crueger
|
---|
6 | */
|
---|
7 |
|
---|
8 | #include "Shapes/BaseShapes.hpp"
|
---|
9 | #include "Shapes/BaseShapes_impl.hpp"
|
---|
10 | #include "Shapes/ShapeOps.hpp"
|
---|
11 |
|
---|
12 | #include "vector.hpp"
|
---|
13 | #include "Helpers/Assert.hpp"
|
---|
14 |
|
---|
15 | #include <cmath>
|
---|
16 | #include <algorithm>
|
---|
17 |
|
---|
18 | bool Sphere_impl::isInside(const Vector &point){
|
---|
19 | return point.NormSquared()<=1;
|
---|
20 | }
|
---|
21 |
|
---|
22 | bool Sphere_impl::isOnSurface(const Vector &point){
|
---|
23 | return fabs(point.NormSquared()-1)<MYEPSILON;
|
---|
24 | }
|
---|
25 |
|
---|
26 | Vector Sphere_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
|
---|
27 | if(!isOnSurface(point)){
|
---|
28 | throw NotOnSurfaceException(__FILE__,__LINE__);
|
---|
29 | }
|
---|
30 | return point;
|
---|
31 | }
|
---|
32 |
|
---|
33 | string Sphere_impl::toString(){
|
---|
34 | return "Sphere()";
|
---|
35 | }
|
---|
36 |
|
---|
37 | Shape Sphere(){
|
---|
38 | Shape::impl_ptr impl = Shape::impl_ptr(new Sphere_impl());
|
---|
39 | return Shape(impl);
|
---|
40 | }
|
---|
41 |
|
---|
42 | Shape Sphere(const Vector ¢er,double radius){
|
---|
43 | return translate(resize(Sphere(),radius),center);
|
---|
44 | }
|
---|
45 |
|
---|
46 | Shape Ellipsoid(const Vector ¢er, const Vector &radius){
|
---|
47 | return translate(stretch(Sphere(),radius),center);
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool Cuboid_impl::isInside(const Vector &point){
|
---|
51 | return fabs(point[0])<=1 && fabs(point[1])<=1 && fabs(point[2])<=1;
|
---|
52 | }
|
---|
53 |
|
---|
54 | bool Cuboid_impl::isOnSurface(const Vector &point){
|
---|
55 | bool retVal = isInside(point);
|
---|
56 | // test all borders of the cuboid
|
---|
57 | // double fabs
|
---|
58 | retVal = retVal &&
|
---|
59 | ((fabs(fabs(point[0])-1) < MYEPSILON) ||
|
---|
60 | (fabs(fabs(point[1])-1) < MYEPSILON) ||
|
---|
61 | (fabs(fabs(point[2])-1) < MYEPSILON));
|
---|
62 | return retVal;
|
---|
63 | }
|
---|
64 |
|
---|
65 | Vector Cuboid_impl::getNormal(const Vector &point) throw(NotOnSurfaceException){
|
---|
66 | if(!isOnSurface(point)){
|
---|
67 | throw NotOnSurfaceException(__FILE__,__LINE__);
|
---|
68 | }
|
---|
69 | Vector res;
|
---|
70 | // figure out on which sides the Vector lies (maximum 3, when it is in a corner)
|
---|
71 | for(int i=NDIM;i--;){
|
---|
72 | if(fabs(fabs(point[i])-1)<MYEPSILON){
|
---|
73 | // add the scaled (-1/+1) Vector to the set of surface vectors
|
---|
74 | res[i] = point[i];
|
---|
75 | }
|
---|
76 | }
|
---|
77 | ASSERT(res.NormSquared()>=1 && res.NormSquared()<=3,"To many or to few sides found for this Vector");
|
---|
78 |
|
---|
79 | res.Normalize();
|
---|
80 | return res;
|
---|
81 | }
|
---|
82 |
|
---|
83 | string Cuboid_impl::toString(){
|
---|
84 | return "Cuboid()";
|
---|
85 | }
|
---|
86 |
|
---|
87 | Shape Cuboid(){
|
---|
88 | Shape::impl_ptr impl = Shape::impl_ptr(new Cuboid_impl());
|
---|
89 | return Shape(impl);
|
---|
90 | }
|
---|
91 |
|
---|
92 | Shape Cuboid(const Vector &corner1, const Vector &corner2){
|
---|
93 | // make sure the two edges are upper left front and lower right back
|
---|
94 | Vector sortedC1;
|
---|
95 | Vector sortedC2;
|
---|
96 | for(int i=NDIM;i--;){
|
---|
97 | sortedC1[i] = min(corner1[i],corner2[i]);
|
---|
98 | sortedC2[i] = max(corner1[i],corner2[i]);
|
---|
99 | ASSERT(corner1[i]!=corner2[i],"Given points for cuboid edges did not define a valid space");
|
---|
100 | }
|
---|
101 | // get the middle point
|
---|
102 | Vector middle = (1./2.)*(sortedC1+sortedC2);
|
---|
103 | Vector factors = sortedC2-middle;
|
---|
104 | return translate(stretch(Cuboid(),factors),middle);
|
---|
105 | }
|
---|