commit cf05e6012ec5d162d92612460f4bbb851a1003ce
parent 2dafdf60dd23ac7abc962687eabc789fd7f5b5cc
Author: Georges Dupéron <jahvascriptmaniac+github@free.fr>
Date: Fri, 6 Jan 2012 20:17:42 +0100
Refactor et correction de offset, qui est maintenant "inset".
Diffstat:
16 files changed, 86 insertions(+), 100 deletions(-)
diff --git a/geometry/directions.hh b/geometry/directions.hh
@@ -43,4 +43,18 @@ inline SommetTriangle operator-(SommetTriangle c, int i) {
return SommetTriangle((((int(c) - int(i)) % 3 ) + 3) % 3);
}
+enum CoteTriangle {
+ LEFTSIDE = 0,
+ RIGHTSIDE = 1,
+ BASE = 2
+};
+
+inline CoteTriangle operator+(CoteTriangle c, int i) {
+ return CoteTriangle((((int(c) + int(i)) % 3 ) + 3) % 3);
+}
+
+inline CoteTriangle operator-(CoteTriangle c, int i) {
+ return CoteTriangle((((int(c) - int(i)) % 3 ) + 3) % 3);
+}
+
#endif
diff --git a/geometry/quad.cpp b/geometry/quad.cpp
@@ -9,17 +9,22 @@ Quad::Quad(Vertex ne, Vertex se, Vertex sw, Vertex nw) {
c[NW] = nw;
}
-void Quad::offset(Cardinal side, int offset) {
- Vertex voffset = (c[NE + side]-c[NW + side]).perpendicular().setNorm(offset);
- c[NE + side] = c[NE + side] + voffset.projectOn(c[NE + side]-c[SE + side]);
- c[NW + side] = c[NW + side] + voffset.projectOn(c[NW + side]-c[SW + side]);
+Quad Quad::inset(Cardinal side, float offset) const {
+ Quad q = (*this) << side;
+ Vertex offsetDirection = (q[NW]-q[NE]).perpendicularCw();
+ float distE = offset / offsetDirection.cosAngle(q[SE] - q[NE]);
+ float distW = offset / offsetDirection.cosAngle(q[SW] - q[NW]);
+ q[NE] = q[NE] + (q[SE] - q[NE]).setNorm(distE);
+ q[NW] = q[NW] + (q[SW] - q[NW]).setNorm(distW);
+ return q >> side;
}
-void Quad::offsetNESW(int offsetN, int offsetE, int offsetS, int offsetW) {
- this->offset(N,offsetN);
- this->offset(E,offsetE);
- this->offset(S,offsetS);
- this->offset(W,offsetW);
+Quad Quad::insetNESW(float offsetN, float offsetE, float offsetS, float offsetW) const {
+ return (*this).inset(N,offsetN).inset(E,offsetE).inset(S,offsetS).inset(W,offsetW);
+}
+
+Quad Quad::insetNESW(float offset) const {
+ return insetNESW(offset, offset, offset, offset);
}
Quad Quad::makeParallelogram() {
@@ -104,3 +109,9 @@ float Quad::maxAngle() {
Quad operator+(const Quad& q, const Vertex& v) {
return Quad(q[NE] + v, q[SE] + v, q[SW] + v, q[NW] + v);
}
+
+void Quad::cutCornerCorner(Coin from) const {
+ Triangle t1(c[from-1], c[from], c[from+1]);
+ Triangle t2(c[from+1], c[from+2], c[from-1]);
+ // TODO
+}
diff --git a/geometry/quad.hh b/geometry/quad.hh
@@ -24,8 +24,9 @@ class Quad {
return Quad(c[NE + rot], c[SE + rot], c[SW + rot], c[NW + rot]);
}
friend Quad operator+(const Quad& t, const Vertex& v);
- void offset(Cardinal side, int offset);
- void offsetNESW(int offsetN, int offsetE, int offsetS, int offsetW);
+ Quad inset(Cardinal side, float offset) const;
+ Quad insetNESW(float offsetN, float offsetE, float offsetS, float offsetW) const;
+ Quad insetNESW(float offset) const;
float minLengthNS();
float minLengthEW();
float maxLengthNS();
@@ -34,6 +35,7 @@ class Quad {
float maxLength();
float minAngle();
float maxAngle();
+ void cutCornerCorner(Coin from) const;
Quad makeParallelogram();
};
diff --git a/geometry/triangle.cpp b/geometry/triangle.cpp
@@ -29,12 +29,10 @@ float Triangle::maxLength() const {
return std::max(std::max((c[0] - c[1]).norm(), (c[1] - c[2]).norm()), (c[2] - c[0]).norm());
}
-void Triangle::offsetBase(int offset) {
- Quad q = Quad(c[1], c[0], c[2], c[1]);
- q.offset(S, -offset);
- c[0] = q[SE];
- c[1] = q[NE];
- c[2] = q[SW];
+Triangle Triangle::inset(CoteTriangle side, float offset) const {
+ Quad q = Quad(c[RIGHT + side], c[LEFT + side], c[TOP + side], c[RIGHT + side]);
+ q = q.inset(S, offset);
+ return (Triangle(q[SE], q[SW], q[NW]) >> side);
}
Triangle operator+(const Triangle& t, const Vertex& v) {
diff --git a/geometry/triangle.hh b/geometry/triangle.hh
@@ -28,7 +28,7 @@ class Triangle {
float minAngle() const; // angle minimum du triangle (en c[0], c[1] ou c[2]).
float minLength() const;
float maxLength() const;
- void offsetBase(int offset);
+ Triangle inset(CoteTriangle side, float offset) const;
void display();
};
diff --git a/geometry/vertex.cpp b/geometry/vertex.cpp
@@ -23,26 +23,30 @@ Vertex intersection(Vertex a, Vertex b, Vertex c, Vertex d) {
);
}
-Vertex Vertex::projectOn(Vertex v) {
+Vertex Vertex::projectOn(Vertex v) const {
// http://www.developpez.net/forums/d202580/applications/developpement-2d-3d-jeux/contribuez/faq-mat-quat-ajout-calculs-vectoriels/
- int64 scalaire = ((int64)this->x)*((int64)v.x) + ((int64)this->y)*((int64)v.y);
- int normecarre = v.norm();
+ float scalaire = (this->x)*(v.x) + (this->y)*(v.y);
+ float normecarre = v.norm();
normecarre *= normecarre;
- return Vertex(((int64)v.x) * scalaire / normecarre, ((int64)v.y) * scalaire / normecarre, 0);
+ return Vertex(v.x * scalaire / normecarre, v.y * scalaire / normecarre, 0);
}
-Vertex Vertex::setNorm(float n) {
- int64 current = norm();
- return Vertex((int64)x * (int64)n / current, (int64)y * (int64)n / current, 0);
+Vertex Vertex::setNorm(float n) const {
+ return (*this * n / norm());
}
-Vertex Vertex::perpendicular() {
+Vertex Vertex::perpendicularCw() const {
return Vertex(-y, x, 0);
}
-float Vertex::cosAngle(Vertex v) {
+float Vertex::cosAngle(Vertex v) const {
// http://www.developpez.net/forums/d202580/applications/developpement-2d-3d-jeux/contribuez/faq-mat-quat-ajout-calculs-vectoriels/
- return ((double)(this->x*v.x + this->y*v.y)) / (((double)norm())*((double)v.norm()));
+ //std::cout << "cosAngle " << *this << " " << v << " " << ((this->x*v.x + this->y*v.y) / (norm()*v.norm())) << " " << (norm()*v.norm()) << std::endl;
+ return ((this->x*v.x + this->y*v.y) / (norm()*v.norm()));
+}
+
+float Vertex::angle(Vertex v) const {
+ return std::acos(cosAngle(v));
}
Vertex::operator Vertex() { return Vertex(x,y,z); }
diff --git a/geometry/vertex.hh b/geometry/vertex.hh
@@ -13,10 +13,11 @@ class Vertex {
Vertex();
Vertex(float x, float y, float z);
float norm() const;
- Vertex projectOn(Vertex v);
- Vertex setNorm(float n);
- Vertex perpendicular(); // Perpendiculaire 2D dans le sens contraire des aiguilles d'une montre.
- float cosAngle(Vertex v); // cosinus de l'angle entre this et v.
+ Vertex projectOn(Vertex v) const;
+ Vertex setNorm(float n) const;
+ Vertex perpendicularCw() const; // Perpendiculaire 2D dans le sens contraire des aiguilles d'une montre (ClockWise).
+ float cosAngle(Vertex v) const; // cosinus de l'angle entre this et v.
+ float angle(Vertex v) const; // Angle entre this et v.
static Vertex fromSpherical(float r, float xAngle, float yAngle);
friend Vertex intersection(Vertex a, Vertex b, Vertex c, Vertex d); // Intersection entre (a,b) et (c,d).
@@ -32,31 +33,4 @@ class Vertex {
friend Vertex operator/(const Vertex& v, const float f);
};
-/*
-class Vertex {
- public :
- float x;
- float y;
- float z;
-
- public :
- Vertex();
- Vertex(float x, float y, float z);
- float norm();
- static Vertex fromSpherical(float r, float xAngle, float yAngle);
-
- public :
- operator Vertex();
- friend std::ostream& operator<<(std::ostream& os, const Vertex& v);
- friend Vertex operator+(const Vertex& u, const Vertex& v);
- friend Vertex operator-(const Vertex& u, const Vertex& v);
- friend Vertex operator-(const Vertex& v);
- friend Vertex operator*(const Vertex& v, const int n);
- friend Vertex operator*(const Vertex& u, const Vertex& v);
- friend Vertex operator/(const Vertex& v, const int n);
- friend Vertex operator/(const Vertex& v, const float f);
- friend Vertex operator+(const Vertex& u, const Vertex& v);
- friend Vertex operator-(const Vertex& u, const Vertex& v);
-};
-*/
#endif
diff --git a/rules/batiment/batimentquad.cpp b/rules/batiment/batimentquad.cpp
@@ -18,11 +18,7 @@ bool BatimentQuad::split() {
addChild(new BatimentQuadMaisonPont(q,800));
} else {
int th = 20; // Terrain height.
- Quad q = Quad(c[NE],c[SE],c[SW],c[NW]);
- q.offset(N,-140);
- q.offset(E,-140);
- q.offset(S,-140);
- q.offset(W,-140);
+ Quad q = Quad(c[NE],c[SE],c[SW],c[NW]).insetNESW(140);
addChild(new TrottoirQuadNormal(Quad(c[NE],c[SE],q[SE],q[NE]),th));
addChild(new TrottoirQuadNormal(Quad(c[SE],c[SW],q[SW],q[SE]),th));
@@ -32,8 +28,7 @@ bool BatimentQuad::split() {
Quad qh = q + Vertex(0,0,th);
addChild(new BatimentQuadJardin(qh));
- qh.offset(this->entry,-400);
- addChild(new BatimentQuadMaison(qh));
+ addChild(new BatimentQuadMaison(qh.inset(this->entry,400)));
}
return true;
}
diff --git a/rules/batiment/batimentquadmaisonpont.cpp b/rules/batiment/batimentquadmaisonpont.cpp
@@ -16,14 +16,10 @@ bool BatimentQuadMaisonPont::split() {
q = q << 1;
float partLength = Segment(q[NE],q[NW]).length() / 3;
int partHeight = 2.5*height/3.;
- Quad qa = q;
- Quad qb = q;
- Quad qc = q;
+ Quad qa = q.inset(E,2*partLength);
+ Quad qb = q.inset(W,2*partLength);
+ Quad qc = q.inset(E, partLength).inset(W, partLength);
Quad qh = q;
- qa.offset(E,-2*partLength);
- qb.offset(W,-2*partLength);
- qc.offset(E, -partLength);
- qc.offset(W, -partLength);
addChild(new BatimentQuadJardin(c));
addChild(new BatimentQuadBlock(qa,partHeight));
diff --git a/rules/batiment/batimentquadpont.cpp b/rules/batiment/batimentquadpont.cpp
@@ -40,7 +40,7 @@ void BatimentQuadPont::triangulation() {
addGPUTriangle(pa,c[NW],ch[NW],0xD0,0xD0,0xD0);
for(var=-1.7,n=0; var <= 1.7; var+=pas,n++) {
- q.offset(W,-n2);
+ q = q.inset(W,n2);
a = q[NW] + Vertex(0,0,nt(var,height));
b = q[SW] + Vertex(0,0,nt(var,height));
diff --git a/rules/chose.cpp b/rules/chose.cpp
@@ -144,4 +144,4 @@ void Chose::drawAABB() {
);
}
-unsigned int Chose::initialSeed = 1417359759;//random_seed();
+unsigned int Chose::initialSeed = random_seed();
diff --git a/rules/quartier/quartierquadangle.cpp b/rules/quartier/quartierquadangle.cpp
@@ -6,15 +6,14 @@ QuartierQuadAngle::QuartierQuadAngle(Quad c) : QuartierQuad(c) {
bool QuartierQuadAngle::split() {
for (int i = 0; i < 4; i++) {
if (Triangle(c[NW+i], c[NE+i], c[SE+i]).angle() >= Angle::d2r(130)) {
- Triangle t1(c[NE+i], c[SE+i], c[SW+i]);
- t1.offsetBase(-hrw);
- Triangle t2(c[SW+i], c[NW+i], c[NE+i]);
- t2.offsetBase(-hrw);
+ // TODO : maintenant que Triangle::offset prend un paramètre side, on peut simplifier ce bazaar.
+ Triangle t1 = Triangle(c[NE+i], c[SE+i], c[SW+i]).inset(BASE, hrw);
+ Triangle t2 = Triangle(c[SW+i], c[NW+i], c[NE+i]).inset(BASE, hrw);
addChild(QuartierTri::factory(seed, 0, t1));
addChild(QuartierTri::factory(seed, 1, t2));
addChild(new RouteQuadChaussee(Quad(t1[LEFT], t1[RIGHT], t2[LEFT], t2[RIGHT])));
- addChild(new RouteTriChaussee(Triangle(t1[LEFT], c[NE+i], t2[RIGHT])));
- addChild(new RouteTriChaussee(Triangle(t2[LEFT], c[SW+i], t1[RIGHT])));
+ addChild(new RouteTriChaussee(Triangle(t2[RIGHT], c[NE+i], t1[LEFT])));
+ addChild(new RouteTriChaussee(Triangle(t1[RIGHT], c[SW+i], t2[LEFT])));
return true;
}
}
@@ -28,23 +27,21 @@ bool QuartierQuadAngle::split() {
Triangle te = Triangle(c[NW+i], c[NE+i], e);
Quad q;
if (tn.minAngle() > te.minAngle()) {
- q = Quad(n, c[SE+i], c[SW+i], c[NW+i]);
+ q = Quad(n, c[SE+i], c[SW+i], c[NW+i]).inset(E, hrw);
Vertex oldtnright = tn[RIGHT];
- tn.offsetBase(-hrw);
- q.offset(E, -hrw);
+ tn = tn.inset(BASE, hrw);
addChild(QuartierTri::factory(seed, 0, tn));
addChild(QuartierQuad::factory(seed, 1, q));
addChild(new RouteQuadChaussee(Quad(tn[LEFT], tn[RIGHT], q[SE], q[NE])));
- addChild(new RouteTriChaussee(Triangle(q[SE], oldtnright, tn[RIGHT])));
+ addChild(new RouteTriChaussee(Triangle(tn[RIGHT], oldtnright, q[SE])));
} else {
- q = Quad(c[NW+i], e, c[SE+i], c[SW+i]);
+ q = Quad(c[NW+i], e, c[SE+i], c[SW+i]).inset(E, hrw);
Vertex oldteleft = te[LEFT];
- te.offsetBase(-hrw);
- q.offset(E, -hrw);
+ te = te.inset(BASE, hrw);
addChild(QuartierTri::factory(seed, 0, te));
addChild(QuartierQuad::factory(seed, 1, q));
addChild(new RouteQuadChaussee(Quad(te[LEFT], te[RIGHT], q[SE], q[NE])));
- addChild(new RouteTriChaussee(Triangle(te[LEFT], oldteleft, q[NE])));
+ addChild(new RouteTriChaussee(Triangle(q[NE], oldteleft, te[LEFT])));
}
return true;
}
diff --git a/rules/quartier/quartierquadcarre.cpp b/rules/quartier/quartierquadcarre.cpp
@@ -16,7 +16,7 @@ bool QuartierQuadCarre::split() {
}
for (int i = 0; i < 4; i++) {
q[i] = Quad(c[NE+i], middle[E+i], center, middle[N+i]);
- q[i].offset(W,-hrw); q[i].offset(S,-hrw);
+ q[i] = q[i].inset(W,hrw).inset(S,hrw);
}
addChild(new RouteQuadCarrefour(Quad(q[0][SW], q[1][SW], q[2][SW], q[3][SW])));
for (int i = 0; i < 4; i++) {
diff --git a/rules/quartier/quartierquadrect.cpp b/rules/quartier/quartierquadrect.cpp
@@ -7,10 +7,8 @@ bool QuartierQuadRect::split() {
Vertex n = Segment(c[NW], c[NE]).randomPos(seed, 0, 33, 67);
Vertex s = Segment(c[SE], c[SW]).randomPos(seed, 1, 33, 67);
- Quad qe = Quad(c[NE], c[SE], s, n);
- Quad qw = Quad(c[SW], c[NW], n, s);
- qe.offset(W,-hrw);
- qw.offset(W,-hrw);
+ Quad qe = Quad(c[NE], c[SE], s, n).inset(W,hrw);
+ Quad qw = Quad(c[SW], c[NW], n, s).inset(W,hrw);
addChild(new RouteQuadChaussee(Quad(qe[NW], qe[SW], qw[NW], qw[SW])));
addChild(QuartierQuad::factory(seed, 2, qe));
diff --git a/rules/quartier/quartiertrihauteur.cpp b/rules/quartier/quartiertrihauteur.cpp
@@ -6,10 +6,8 @@ QuartierTriHauteur::QuartierTriHauteur(Triangle c) : QuartierTri(c) {
bool QuartierTriHauteur::split() {
Vertex baseCenter = Segment(c[LEFT], c[RIGHT]).randomPos(seed, 0, 33, 67);
- Triangle tl = Triangle(baseCenter, c[LEFT], c[TOP]);
- Triangle tr = Triangle(c[TOP], c[RIGHT], baseCenter);
- tl.offsetBase(-hrw);
- tr.offsetBase(-hrw);
+ Triangle tl = Triangle(baseCenter, c[LEFT], c[TOP]).inset(BASE, hrw);
+ Triangle tr = Triangle(c[TOP], c[RIGHT], baseCenter).inset(BASE, hrw);
addChild(new RouteQuadChaussee(Quad(tr[LEFT], tr[RIGHT], tl[LEFT], tl[RIGHT])));
addChild(new RouteTriChaussee(Triangle(tl[RIGHT], c[TOP], tr[LEFT])));
diff --git a/rules/route/trottoirquadnormal.cpp b/rules/route/trottoirquadnormal.cpp
@@ -10,8 +10,7 @@ void TrottoirQuadNormal::getBoundingBoxPoints() {
void TrottoirQuadNormal::triangulation() {
Quad ch = c + Vertex(0,0,height);
- Quad bordureh = ch;
- bordureh.offset(E,-15);
+ Quad bordureh = ch.inset(E,15);
addGPUQuad(c[NE], c[SE], ch[SE], ch[NE], 0xAA, 0xAA, 0xAA);
addGPUQuad(ch[NE], ch[SE], bordureh[SE], bordureh[NE], 0xAA, 0xAA, 0xAA);