-
Notifications
You must be signed in to change notification settings - Fork 0
/
3DPlanes.h
74 lines (66 loc) · 2 KB
/
3DPlanes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#pragma once
#include <vector>
#include <memory>
#include "simulation.h"
class VanishPlane;
typedef sf::Vector2f (*TransformFunc)(const VanishPlane&, const sf::Vector2f&);
sf::Vector2f transformPointSide(const VanishPlane&, const sf::Vector2f&);
sf::Vector2f transformPointFloor(const VanishPlane&, const sf::Vector2f&);
sf::Vector2f transformPointBack(const VanishPlane&, const sf::Vector2f&);
sf::Vector2f noTransform(const VanishPlane&, const sf::Vector2f&);
class VanishPlane
{
public:
TransformFunc m_transform;
sf::Vector2f vanishingPoint;
float intersect;
sf::Vector2f translate;
VanishPlane(
TransformFunc func=&noTransform,
const sf::Vector2f& vanish=sf::Vector2f(0, 0),
float intersect=0,
const sf::Vector2f& translate = sf::Vector2f(0, 0))
:m_transform(func)
,vanishingPoint(vanish)
,intersect(intersect)
,translate(translate)
{}
sf::Vector2f transformPoint(const sf::Vector2f& point) const
{
return m_transform(*this, point + translate);
}
sf::Vector2f operator() (const sf::Vector2f& point) const
{
return transformPoint(point);
}
static VanishPlane sidePlane(
const sf::Vector2f& vanishingPoint,
float intersect,
const sf::Vector2f& translate = sf::Vector2f(0, 0))
{
return VanishPlane(&transformPointSide, vanishingPoint, intersect, translate);
}
static VanishPlane floorPlane(
const sf::Vector2f& vanishingPoint,
float intersect,
const sf::Vector2f& translate = sf::Vector2f(0, 0))
{
return VanishPlane(&transformPointFloor, vanishingPoint, intersect, translate);
}
static VanishPlane backPlane(
const sf::Vector2f& vanishingPoint,
float intersect,
const sf::Vector2f& translate = sf::Vector2f(0, 0))
{
return VanishPlane(&transformPointBack, vanishingPoint, intersect, translate);
}
};
template<class Shape, class Transform>
Shape transformShape(const Shape& shape, Transform&& transform)
{
Shape copy(shape);
auto vertexCount = copy->m_vertexCount;
for(int i = 0; i < vertexCount; ++i)
copy.setPoint(i, transform(copy.getPoint(i)));
return shape;
}