refactoring/cleanups

This commit is contained in:
2018-07-30 20:56:17 +02:00
parent 9a07790ec6
commit a9fcb77675
23 changed files with 276 additions and 247 deletions

View File

@@ -37,7 +37,7 @@ void MV2DElementFloorObstacleDoor::paint(Painter& p) {
// }
QPen pen;
pen.setColor(QColor(0.5,0.5,0.5));
pen.setColor(QColor(255,0,0));
pen.setStyle(Qt::PenStyle::DotLine);
p.setPenBrush(pen, Qt::NoBrush);

View File

@@ -7,6 +7,22 @@
#include <Indoor/floorplan/v2/Floorplan.h>
#include <stdio.h>
float getPosOnLine(const Floorplan::FloorObstacleWall* wall, const Point2 pos) {
Point2 perp = (wall->from - wall->to).perpendicular() * 100;
Line2 l1(pos-perp, pos+perp);
Line2 l2(wall->from, wall->to);
Point2 p;
float u = 0;
bool isects = intersects(l2, l1, true, p, &u);
if (u < 0.01) {u = 0.01;}
if (u > 0.99) {u = 0.99;}
return (isects) ? (u) : (NAN);
}
void drawDoorSwing(const Floorplan::FloorObstacleWall* wall, const Floorplan::FloorObstacleWallDoor* door, Painter& p, QPen& pen) {
@@ -126,8 +142,15 @@ ClickDist MV2DElementFloorObstacleWall::getMinDistanceXY(const Point2 p) const {
void MV2DElementFloorObstacleWall::mousePressed(MapView2D* v, const Point2 p) {
(void) v;
this->mouseNearLine = getPosOnLine(fo, p);
}
void MV2DElementFloorObstacleWall::paint(Painter& p) {
// convert wall's thickness from meter to pixels
const float thickness_px = p.s.ms(fo->thickness_m);
@@ -157,7 +180,7 @@ void MV2DElementFloorObstacleWall::paint(Painter& p) {
pts.push_back(fo->to);
auto comp = [&] (const Point2 p1, const Point2 p2) {
return pts.front().getDistance(p1) < pts.front().getDistance(p2);
return fo->from.getDistance(p1) < fo->from.getDistance(p2);
};
std::sort(pts.begin(), pts.end(), comp);
@@ -197,7 +220,14 @@ void MV2DElementFloorObstacleWall::paint(Painter& p) {
// obstacle length
p.setPenBrush(Qt::black, Qt::NoBrush);
p.drawLength(fo->from, fo->to, fo->from.getDistance(fo->to), thickness_px/2);
//p.drawLength(fo->from, fo->to, fo->from.getDistance(fo->to), thickness_px/2);
for (size_t i = 0; i < pts.size(); i+=1) {
const Point2 p1 = pts[i];
const Point2 p2 = pts[i+1];
const float len = p2.getDistance(p1);
p.drawLength(p1, p2, len, thickness_px/2);
}
}
@@ -245,21 +275,23 @@ void MV2DElementFloorObstacleWall::onNodeMove(MapView2D* v, const int userIdx, c
if (userIdx == 0) {fo->from.x = newPos.x; fo->from.y = newPos.y;}
if (userIdx == 1) {fo->to.x = newPos.x; fo->to.y = newPos.y;}
Point2 perp = (fo->from - fo->to).perpendicular() * 100;
Line2 l1(newPos-perp, newPos+perp);
Line2 l2(fo->from, fo->to);
// Point2 perp = (fo->from - fo->to).perpendicular() * 100;
// Line2 l1(newPos-perp, newPos+perp);
// Line2 l2(fo->from, fo->to);
Point2 p;
float u = 0;
bool isects = intersects(l2, l1, true, p, &u);
if (u < 0.01) {u = 0.01;}
if (u > 0.99) {u = 0.99;}
// Point2 p;
// float u = 0;
// bool isects = intersects(l2, l1, true, p, &u);
// if (u < 0.01) {u = 0.01;}
// if (u > 0.99) {u = 0.99;}
const float u = getPosOnLine(fo, newPos);
if (userIdx >= 1000 && userIdx < 2000) {
Floorplan::FloorObstacleWallDoor* door = fo->doors[userIdx-1000];
if (isects) {
if (!std::isnan(u)) {
door->atLinePos = u;
}
@@ -267,7 +299,7 @@ void MV2DElementFloorObstacleWall::onNodeMove(MapView2D* v, const int userIdx, c
Floorplan::FloorObstacleWallWindow* win = fo->windows[userIdx-2000];
if (isects) {
if (!std::isnan(u)) {
win->atLinePos = u;
}
@@ -285,7 +317,10 @@ void MV2DElementFloorObstacleWall::onNodeMoved(MapView2D* v, const int userIdx,
bool MV2DElementFloorObstacleWall::keyPressEvent(MapView2D* v, QKeyEvent* e) {
if (e->key() == Qt::Key_D) {
Floorplan::FloorObstacleWallDoor* door = new Floorplan::FloorObstacleWallDoor(Floorplan::DoorType::SWING, Floorplan::Material::WOOD, 0.5, 1.0, 2.1);
const float doorWidth = 0.9;
const float doorHeight = 2.1;
const float doorAt = std::isnan(mouseNearLine) ? (0.5) : (mouseNearLine);
Floorplan::FloorObstacleWallDoor* door = new Floorplan::FloorObstacleWallDoor(Floorplan::DoorType::SWING, Floorplan::Material::WOOD, doorAt, doorWidth, doorHeight);
fo->doors.push_back(door);
return true;
} else if (e->key() == Qt::Key_W) {

View File

@@ -14,6 +14,8 @@ private:
Floorplan::Floor* f;
Floorplan::FloorObstacleWall* fo;
float mouseNearLine = NAN;
public:
/** ctor */
@@ -40,6 +42,8 @@ public:
bool keyPressEvent(MapView2D* v, QKeyEvent* e) override;
void mousePressed(MapView2D* v, const Point2 p) override;
};
#endif // MV2DELEMENTFLOOROBSTACLEWALL_H

View File

@@ -154,7 +154,7 @@ void Painter::drawImage(const Point2 pt, const QImage& img) {
void Painter::drawLength(Point2 p1, Point2 p2, const float len, const float offset) {
if (p1.x < p2.x) {swap(p1, p2);}
const Point2 center_m = (p1 + p2) / 2;
const Point2 center_m = (p1 + p2) / 2 - Point2(0.5,0);
Point2 dir_px = (p2 - p1).perpendicular().normalized() * (5+offset);
if (dir_px.x <= 0) {dir_px = -dir_px;}
const Point2 pos_m = center_m + dir_px / getScaler().getScale();