| 158 | } |
| 159 | |
| 160 | std::optional<Primitives::Variant> primitiveFromObject( const Object& object ) |
| 161 | { |
| 162 | if ( auto point = dynamic_cast<const PointObject*>( &object ) ) |
| 163 | { |
| 164 | return toPrimitive( point->getPoint() ); |
| 165 | } |
| 166 | else if ( auto line = dynamic_cast<const LineObject*>( &object ) ) |
| 167 | { |
| 168 | return toPrimitive( LineSegm3f( line->getPointA(), line->getPointB() ) ); |
| 169 | } |
| 170 | else if ( auto plane = dynamic_cast<const PlaneObject*>( &object ) ) |
| 171 | { |
| 172 | return Primitives::Plane{ .center = plane->getCenter(), .normal = plane->getNormal()/* Already normalized. */ }; |
| 173 | } |
| 174 | else if ( auto sphere = dynamic_cast<const SphereObject*>( &object ) ) |
| 175 | { |
| 176 | return toPrimitive( Sphere( sphere->getCenter(), sphere->getRadius() ) ); |
| 177 | } |
| 178 | else if ( auto circle = dynamic_cast<const CircleObject*>( &object ) ) |
| 179 | { |
| 180 | float radius = circle->getRadius(); |
| 181 | return Primitives::ConeSegment{ |
| 182 | .referencePoint = circle->getCenter(), |
| 183 | .dir = circle->getNormal(), |
| 184 | .positiveSideRadius = radius, |
| 185 | .negativeSideRadius = radius, |
| 186 | .hollow = true, |
| 187 | }; |
| 188 | } |
| 189 | else if ( auto cyl = dynamic_cast<const CylinderObject*>( &object ) ) |
| 190 | { |
| 191 | float radius = cyl->getRadius(); |
| 192 | float halfLen = cyl->getLength() / 2; |
| 193 | return Primitives::ConeSegment{ |
| 194 | .referencePoint = cyl->getCenter(), |
| 195 | .dir = cyl->getDirection(), |
| 196 | .positiveSideRadius = radius, |
| 197 | .negativeSideRadius = radius, |
| 198 | .positiveLength = halfLen, |
| 199 | .negativeLength = halfLen, |
| 200 | .hollow = true, // I guess? |
| 201 | }; |
| 202 | } |
| 203 | else if ( auto cone = dynamic_cast<const ConeObject*>( &object ) ) |
| 204 | { |
| 205 | // I want the "positive" direction to point towards the tip (so "axis -> positive/negative end"). |
| 206 | // It's moot where the center should be, but currently having it at the tip is ok. |
| 207 | Primitives::ConeSegment ret{ |
| 208 | .referencePoint = cone->getCenter(), |
| 209 | .dir = -cone->getDirection(), |
| 210 | .positiveSideRadius = 0, |
| 211 | .negativeSideRadius = cone->getBaseRadius(), |
| 212 | .positiveLength = 0, |
| 213 | .negativeLength = cone->getHeight(), |
| 214 | .hollow = true, // I guess? |
| 215 | }; |
| 216 | return ret; |
| 217 | } |
no test coverage detected