Compoile fixes
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
#include <core/object.h>
|
||||
#include <scene/3d/immediate_geometry.h>
|
||||
#include "wedge.h"
|
||||
#include "base_data.h"
|
||||
#include "contours.h"
|
||||
|
||||
Contours *Contours::singleton = nullptr;
|
||||
@@ -17,6 +18,7 @@ Contours *Contours::get_singleton()
|
||||
Contours::Contours()
|
||||
: dbg(nullptr)
|
||||
{
|
||||
BaseData::get_singleton()->get().component<Lot>();
|
||||
}
|
||||
|
||||
Contours::~Contours()
|
||||
@@ -84,41 +86,107 @@ void Contours::build()
|
||||
}
|
||||
contours.push_back(mc);
|
||||
}
|
||||
polygons.clear();
|
||||
for (i = 0; i < (int)contours.size(); i++)
|
||||
polygons.push_back({ contours[i].points, contours[i].aabb });
|
||||
List<struct polygon> polygon_queue;
|
||||
List<struct polygon> polygon_output;
|
||||
for (i = 0; i < (int)polygons.size(); i++)
|
||||
polygon_queue.push_back(polygons[i]);
|
||||
polygons.clear();
|
||||
List<struct polygon>::Element *e = polygon_queue.front();
|
||||
while (e) {
|
||||
struct polygon item = e->get();
|
||||
print_line("queue: " + itos(polygon_queue.size()));
|
||||
polygon_queue.pop_front();
|
||||
float area = item.area();
|
||||
if (area > 120.0f * 120.0f && item.aabb.size.x > 10.0f &&
|
||||
item.aabb.size.z > 10.0f) {
|
||||
std::pair<struct polygon, struct polygon> polys =
|
||||
item.split();
|
||||
if (polys.first.area() < 10.0f ||
|
||||
polys.second.area() < 10.0f) {
|
||||
polygon_output.push_back(item);
|
||||
} else {
|
||||
polygon_queue.push_back(polys.first);
|
||||
polygon_queue.push_back(polys.second);
|
||||
flecs::entity base_e = BaseData::get_singleton()->get().lookup("lots");
|
||||
if (base_e.is_valid())
|
||||
base_e.destruct();
|
||||
base_e = BaseData::get_singleton()->get().entity("lots");
|
||||
List<struct Lot::polygon> polygon_queue;
|
||||
List<struct Lot::polygon> polygon_output;
|
||||
Vector<struct Lot::polygon> check;
|
||||
check.resize(contours.size());
|
||||
/* checking contours do not intersect */
|
||||
for (i = 0; i < (int)contours.size(); i++) {
|
||||
check.write[i] = { contours[i].points, contours[i].aabb };
|
||||
}
|
||||
Set<int> ignore;
|
||||
int k, l;
|
||||
for (i = 0; i < (int)contours.size(); i++) {
|
||||
for (j = 0; j < (int)check.size(); j++) {
|
||||
if (i != j) {
|
||||
for (k = 0; k < (int)contours[i].points.size();
|
||||
k++) {
|
||||
for (l = 0;
|
||||
l < (int)contours[j].points.size();
|
||||
l++) {
|
||||
assert(!contours[i].points[k].is_equal_approx(
|
||||
contours[j].points[l]));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else
|
||||
polygon_output.push_back(item);
|
||||
e = polygon_queue.front();
|
||||
}
|
||||
}
|
||||
polygons.reserve(polygon_output.size());
|
||||
e = polygon_output.front();
|
||||
while (e) {
|
||||
polygons.push_back(e->get());
|
||||
e = e->next();
|
||||
for (i = 0; i < (int)contours.size(); i++) {
|
||||
check.write[i] = { contours[i].points, contours[i].aabb };
|
||||
check.write[i].update_aabb();
|
||||
}
|
||||
for (i = 0; i < check.size(); i++) {
|
||||
for (j = 0; j < (int)check.size(); j++) {
|
||||
if (i == j)
|
||||
continue;
|
||||
check[i].intersects(&check[j]);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < (int)check.size(); i++) {
|
||||
polygon_queue.push_back(check[i]);
|
||||
#if 0
|
||||
polygon_output.push_back(check[i]);
|
||||
#endif
|
||||
}
|
||||
#if 0
|
||||
for (i = 0; i < (int)contours.size(); i++)
|
||||
polygon_queue.push_back(
|
||||
{ contours[i].points, contours[i].aabb });
|
||||
#endif
|
||||
{
|
||||
List<struct Lot::polygon>::Element *e = polygon_queue.front();
|
||||
while (e) {
|
||||
struct Lot::polygon item = e->get();
|
||||
print_line("queue: " + itos(polygon_queue.size()));
|
||||
polygon_queue.pop_front();
|
||||
float area = item.area();
|
||||
if (area > 120.0f * 120.0f &&
|
||||
item.aabb.size.x > 10.0f &&
|
||||
item.aabb.size.z > 10.0f) {
|
||||
std::pair<struct Lot::polygon,
|
||||
struct Lot::polygon>
|
||||
polys = item.split();
|
||||
if (polys.first.area() < 10.0f ||
|
||||
polys.second.area() < 10.0f) {
|
||||
polygon_output.push_back(item);
|
||||
} else {
|
||||
polygon_queue.push_back(polys.first);
|
||||
polygon_queue.push_back(polys.second);
|
||||
}
|
||||
} else
|
||||
polygon_output.push_back(item);
|
||||
e = polygon_queue.front();
|
||||
}
|
||||
e = polygon_output.front();
|
||||
while (e) {
|
||||
flecs::entity lot_e = BaseData::get_singleton()
|
||||
->get()
|
||||
.entity()
|
||||
.child_of(base_e);
|
||||
lot_e.set<Lot>({ e->get() });
|
||||
e = e->next();
|
||||
}
|
||||
}
|
||||
BaseData::get_singleton()->get().query_builder<Lot>().build().each(
|
||||
[&](flecs::entity e, Lot &lot) { lot.polygon.update_aabb(); });
|
||||
BaseData::get_singleton()->get().query_builder<Lot>().build().each(
|
||||
[&](flecs::entity e, Lot &lot) {
|
||||
BaseData::get_singleton()
|
||||
->get()
|
||||
.query_builder<Lot>()
|
||||
.build()
|
||||
.each([&](flecs::entity we, Lot &wlot) {
|
||||
if (e != we) {
|
||||
assert(!lot.polygon.intersects(
|
||||
&wlot.polygon));
|
||||
}
|
||||
});
|
||||
});
|
||||
Lot::pack();
|
||||
}
|
||||
|
||||
void Contours::debug()
|
||||
@@ -155,124 +223,102 @@ void Contours::debug()
|
||||
}
|
||||
}
|
||||
dbg->end();
|
||||
flecs::entity base_e = BaseData::get_singleton()->get().lookup("lots");
|
||||
assert(base_e.is_valid());
|
||||
flecs::query<Lot> query = BaseData::get_singleton()
|
||||
->get()
|
||||
.query_builder<Lot>()
|
||||
.with(flecs::ChildOf, base_e)
|
||||
.build();
|
||||
dbg->begin(Mesh::PRIMITIVE_LINES);
|
||||
dbg->set_color(Color(1, 1, 0.4f, 1));
|
||||
for (i = 0; i < (int)polygons.size(); i++) {
|
||||
for (j = 0; j < (int)polygons[i].points.size(); j++) {
|
||||
dbg->add_vertex(polygons[i].points[j] +
|
||||
query.each([&](flecs::entity e, const Lot &lot) {
|
||||
for (j = 0; j < (int)lot.polygon.points.size(); j++) {
|
||||
dbg->add_vertex(lot.polygon.points[j] +
|
||||
Vector3(0, 2, 0));
|
||||
dbg->add_vertex(
|
||||
polygons[i].points[(j + 1) %
|
||||
polygons[i].points.size()] +
|
||||
lot.polygon.points[(j + 1) %
|
||||
lot.polygon.points.size()] +
|
||||
Vector3(0, 2, 0));
|
||||
List<Pair<AABB, String> > lot_data;
|
||||
Lot::get_lot_data(e, &lot_data);
|
||||
List<Pair<AABB, String> >::Element *me =
|
||||
lot_data.front();
|
||||
while (me) {
|
||||
const Pair<AABB, String> &data = me->get();
|
||||
dbg->add_vertex(data.first.get_center());
|
||||
dbg->add_vertex(data.first.get_center() +
|
||||
Vector3(0, 50, 0));
|
||||
me = me->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
dbg->end();
|
||||
dbg->begin(Mesh::PRIMITIVE_LINES);
|
||||
dbg->set_color(Color(1, 1, 0.3, 1));
|
||||
query.each([&](flecs::entity e, const Lot &lot) {
|
||||
for (i = 0; i < lot.polygon.intersections.size(); i++) {
|
||||
for (j = 0; j < lot.polygon.intersections[i].size();
|
||||
j++) {
|
||||
int i0 = j;
|
||||
int i1 = (j + 1) %
|
||||
lot.polygon.intersections.size();
|
||||
Vector2 p0 = lot.polygon.intersections[i][i0];
|
||||
Vector2 p1 = lot.polygon.intersections[i][i1];
|
||||
dbg->add_vertex(Vector3(p0.x, 50, p0.y));
|
||||
dbg->add_vertex(Vector3(p1.x, 50, p1.y));
|
||||
}
|
||||
assert(false);
|
||||
}
|
||||
});
|
||||
dbg->end();
|
||||
dbg->begin(Mesh::PRIMITIVE_LINES);
|
||||
dbg->set_color(Color(0.8f, 0.5f, 0.9f, 1));
|
||||
query.each([&](flecs::entity e, const Lot &lot) {
|
||||
Vector<Pair<Rect2, String> > buildings;
|
||||
Lot::get_lot_buildings(e, &buildings);
|
||||
Vector<Vector<Vector2> > polys = Lot::get_lot_polygons(e);
|
||||
for (i = 0; i < (int)buildings.size(); i++) {
|
||||
const Rect2 r = buildings[i].first;
|
||||
Vector2 mp0 = r.position;
|
||||
Vector2 mp1 = r.position + Vector2(r.size.x, 0);
|
||||
Vector2 mp2 = r.position + r.size;
|
||||
Vector2 mp3 = r.position + Vector2(0, r.size.y);
|
||||
Vector2 rect[] = { mp0, mp1, mp2, mp3 };
|
||||
Vector2 c = r.get_center();
|
||||
dbg->set_color(Color(0.3f, 0.1f, 0.9f, 1));
|
||||
for (j = 0; j < 4; j++) {
|
||||
int i0 = j;
|
||||
int i1 = (j + 1) % 4;
|
||||
Vector3 p0 = Vector3(rect[i0].x, 0, rect[i0].y);
|
||||
Vector3 p1 = Vector3(rect[i1].x, 0, rect[i1].y);
|
||||
dbg->add_vertex(p0);
|
||||
dbg->add_vertex(p1);
|
||||
dbg->add_vertex(p0 + Vector3(0, -10, 0));
|
||||
dbg->add_vertex(p0 + Vector3(0, +10, 0));
|
||||
}
|
||||
#if 0
|
||||
dbg->set_color(Color(0.1f, 0.1f, 1.0f, 1));
|
||||
dbg->add_vertex(Vector3(c.x, 0, c.y));
|
||||
dbg->add_vertex(lot.polygon.points[0]);
|
||||
#endif
|
||||
dbg->set_color(Color(0.3f, 0.1f, 0.9f, 1));
|
||||
assert(polys[i].size() > 0);
|
||||
for (j = 0; j < (int)polys[i].size(); j++) {
|
||||
int i0 = j;
|
||||
int i1 = (j + 1) % (int)polys[i].size();
|
||||
Vector3 p0 = Vector3(polys[i][i0].x, 100,
|
||||
polys[i][i0].y);
|
||||
Vector3 p1 = Vector3(polys[i][i1].x, 100,
|
||||
polys[i][i1].y);
|
||||
dbg->add_vertex(p0);
|
||||
dbg->add_vertex(p1);
|
||||
}
|
||||
dbg->set_color(Color(1.0f, 0.1f, 0.1f, 1));
|
||||
dbg->add_vertex(Vector3(c.x, 0, c.y));
|
||||
dbg->add_vertex(
|
||||
Vector3(polys[i][0].x, 100, polys[i][0].y));
|
||||
}
|
||||
});
|
||||
dbg->end();
|
||||
}
|
||||
|
||||
std::pair<struct Contours::polygon, struct Contours::polygon>
|
||||
Contours::polygon::split() const
|
||||
{
|
||||
int i, j;
|
||||
float split_polygons_area = 0.0f;
|
||||
float split_polygons_area_min = 0.0f;
|
||||
std::pair<struct Contours::polygon, struct Contours::polygon>
|
||||
split_polygons;
|
||||
std::vector<int> edges;
|
||||
for (i = 0; i < (int)points.size(); i++) {
|
||||
Vector3 p0 = points[i];
|
||||
Vector3 p1 = points[(i + 1) % points.size()];
|
||||
if (p0.distance_squared_to(p1) > 100 * 100)
|
||||
edges.push_back(i);
|
||||
}
|
||||
if (edges.size() == 0) {
|
||||
for (i = 0; i < (int)points.size(); i++) {
|
||||
Vector3 p0 = points[i];
|
||||
Vector3 p1 = points[(i + 1) % points.size()];
|
||||
edges.push_back(i);
|
||||
}
|
||||
}
|
||||
for (i = 0; i < (int)edges.size(); i++) {
|
||||
Vector3 p0 = points[edges[i]];
|
||||
Vector3 p1 = points[(edges[i] + 1) % points.size()];
|
||||
Transform xf =
|
||||
Transform(Basis(), p0).looking_at(p1, Vector3(0, 1, 0));
|
||||
AABB base;
|
||||
base.position = xf.xform_inv(p0);
|
||||
base.expand_to(xf.xform_inv(p1));
|
||||
for (j = 0; j < (int)points.size(); j++)
|
||||
base.expand_to(xf.xform_inv(points[j]));
|
||||
Vector3 mpos0, mpos1;
|
||||
if (base.size.x > base.size.z) {
|
||||
mpos0 = base.position +
|
||||
Vector3(base.size.x, 0, 0) * 0.5f;
|
||||
mpos1 = mpos0 + Vector3(0, 0, base.size.z);
|
||||
mpos0 = xf.xform(mpos0);
|
||||
mpos1 = xf.xform(mpos1);
|
||||
} else {
|
||||
mpos0 = base.position +
|
||||
Vector3(0, 0, base.size.z) * 0.5f;
|
||||
mpos1 = mpos0 + Vector3(base.size.x, 0, 0);
|
||||
mpos0 = xf.xform(mpos0);
|
||||
mpos1 = xf.xform(mpos1);
|
||||
}
|
||||
mpos0.y = 0;
|
||||
mpos1.y == 0;
|
||||
/* mpos0 is at start of cut */
|
||||
|
||||
Vector3 n =
|
||||
(mpos1 - mpos0).cross(Vector3(0, 1, 0)).normalized();
|
||||
Plane pl1(mpos0 - n * 1.5f, n);
|
||||
Plane pl2(mpos0 + n * 1.5f, -n);
|
||||
Vector<Vector3> pdata;
|
||||
pdata.resize(points.size());
|
||||
memcpy(pdata.ptrw(), points.data(),
|
||||
points.size() * sizeof(Vector3));
|
||||
struct polygon poly1, poly2;
|
||||
Vector<Vector3> rpdata = Geometry::clip_polygon(pdata, pl1);
|
||||
poly1.points.resize(rpdata.size());
|
||||
memcpy(poly1.points.data(), rpdata.ptr(),
|
||||
rpdata.size() * sizeof(Vector3));
|
||||
poly1.update_aabb();
|
||||
rpdata = Geometry::clip_polygon(pdata, pl2);
|
||||
poly2.points.resize(rpdata.size());
|
||||
memcpy(poly2.points.data(), rpdata.ptr(),
|
||||
rpdata.size() * sizeof(Vector3));
|
||||
poly2.update_aabb();
|
||||
float area1 = poly1.area();
|
||||
float area2 = poly2.area();
|
||||
if (area1 <= 4.0f || area2 <= 4.0f)
|
||||
continue;
|
||||
float area_min = MIN(area1, area2);
|
||||
float area = area1 + area2;
|
||||
if (split_polygons_area_min < area_min &&
|
||||
split_polygons_area < area) {
|
||||
split_polygons = { poly1, poly2 };
|
||||
split_polygons_area_min = area_min;
|
||||
split_polygons_area = area;
|
||||
}
|
||||
}
|
||||
return split_polygons;
|
||||
}
|
||||
|
||||
int Contours::polygon::longest_edge() const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Contours::polygon::update_aabb()
|
||||
{
|
||||
aabb = AABB();
|
||||
int i;
|
||||
for (i = 0; i < (int)points.size(); i++) {
|
||||
if (aabb.is_equal_approx(AABB()))
|
||||
aabb.position = points[i];
|
||||
else
|
||||
aabb.expand_to(points[i]);
|
||||
}
|
||||
}
|
||||
|
||||
float Contours::polygon::area() const
|
||||
{
|
||||
return Geometry::find_polygon_area(points.data(), points.size());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user