Skip to content

Commit

Permalink
Refactor|LineDef: Made private mode LineDef instance data
Browse files Browse the repository at this point in the history
Vertexes/direction vector/accurate length
  • Loading branch information
danij-deng committed Apr 10, 2013
1 parent 8e6b130 commit 3837717
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 126 deletions.
36 changes: 17 additions & 19 deletions doomsday/client/include/map/linedef.h
Expand Up @@ -22,13 +22,17 @@
#define LIBDENG_MAP_LINEDEF

#include <de/binangle.h>
#include <de/mathutil.h> // Divline

#include <de/Vector>
#include <de/Error>
#include "resource/r_data.h"

//#include "resource/r_data.h"

#include "MapElement"
#include "map/r_world.h"
#include "p_mapdata.h"
//#include "p_mapdata.h"

#include "p_dmu.h"
#include "MapElement"

class Vertex;
class Sector;
Expand Down Expand Up @@ -125,7 +129,7 @@ class LineDef : public de::MapElement
int _shadowVisCount;

public:
Side();
Side(Sector *sector = 0);

/**
* Returns @c true iff a Sector is attributed to the side.
Expand Down Expand Up @@ -196,10 +200,6 @@ class LineDef : public de::MapElement
};

public: /// @todo make private:
/// Vertexes:
Vertex *_v1;
Vertex *_v2;

/// Links to vertex line owner nodes:
LineOwner *_vo1;
LineOwner *_vo2;
Expand All @@ -213,16 +213,12 @@ class LineDef : public de::MapElement
/// Calculated from the direction vector.
binangle_t _angle;

/// Direction vector from Start to End vertex.
vec2d_t _direction;

/// Accurate length.
coord_t _length;

int _validCount;

public:
LineDef();
LineDef(Vertex &v1, Vertex &v2,
Sector *frontSector = 0,
Sector *backSector = 0);

/**
* Returns @c true iff the line is part of some Polyobj.
Expand Down Expand Up @@ -573,7 +569,7 @@ class LineDef : public de::MapElement
/**
* Returns a direction vector for the line from Start to End vertex.
*/
const_pvec2d_t &direction() const;
de::Vector2d const &direction() const;

/**
* Returns the logical @em slopetype for the line (which, is determined
Expand Down Expand Up @@ -640,7 +636,8 @@ class LineDef : public de::MapElement
*/
inline coord_t pointDistance(const_pvec2d_t point, coord_t *offset) const
{
return V2d_PointLineDistance(point, v1().origin(), direction(), offset);
coord_t v1Direction[2] = { direction().x, direction().y };
return V2d_PointLineDistance(point, v1().origin(), v1Direction, offset);
}

/// @copydoc pointDistance()
Expand All @@ -662,7 +659,8 @@ class LineDef : public de::MapElement
*/
inline coord_t pointOnSide(const_pvec2d_t point) const
{
return V2d_PointOnLineSide(point, v1().origin(), direction());
coord_t v1Direction[2] = { direction().x, direction().y };
return V2d_PointOnLineSide(point, v1().origin(), v1Direction);
}

/// @copydoc pointOnSide()
Expand Down
68 changes: 28 additions & 40 deletions doomsday/client/src/edit_map.cpp
Expand Up @@ -99,9 +99,10 @@ class EditableMap
return vtx;
}

LineDef *createLine()
LineDef *createLine(Vertex &v1, Vertex &v2, Sector *frontSector = 0,
Sector *backSector = 0)
{
LineDef *line = new LineDef;
LineDef *line = new LineDef(v1, v2, frontSector, backSector);

lines.append(line);
line->setOrigIndex(lines.count()); // 1-based index, 0 = NIL.
Expand Down Expand Up @@ -1002,15 +1003,7 @@ boolean MPE_End()
while(!editMap.lines.isEmpty())
{
map->_lines.append(editMap.lines.takeFirst());
LineDef *line = map->_lines.back();

/// @todo This init should already have been done elsewhere. -ds
line->updateSlopeType();
line->updateAABox();

line->_length = V2d_Length(line->_direction);
line->_angle = bamsAtan2(int( line->_direction[VY] ),
int( line->_direction[VX] ));
map->_lines.back();
}

// Collate polyobjs:
Expand Down Expand Up @@ -1171,13 +1164,13 @@ uint MPE_SidedefCreate(short flags, ddstring_t const *topMaterialUri,
}

#undef MPE_LinedefCreate
uint MPE_LinedefCreate(uint v1, uint v2, uint frontSector, uint backSector,
uint MPE_LinedefCreate(uint v1, uint v2, uint frontSectorIdx, uint backSectorIdx,
uint frontSide, uint backSide, int flags)
{
if(!editMapInited) return 0;

if(frontSector > (uint)editMap.sectors.count()) return 0;
if(backSector > (uint)editMap.sectors.count()) return 0;
if(frontSectorIdx > (uint)editMap.sectors.count()) return 0;
if(backSectorIdx > (uint)editMap.sectors.count()) return 0;
if(frontSide > (uint)editMap.sideDefs.count()) return 0;
if(backSide > (uint)editMap.sideDefs.count()) return 0;
if(v1 == 0 || v1 > (uint)editMap.vertexes.count()) return 0;
Expand All @@ -1191,55 +1184,50 @@ uint MPE_LinedefCreate(uint v1, uint v2, uint frontSector, uint backSector,
return 0;

// Next, check the length is not zero.
/// @todo fixme: We need to allow these... -ds
Vertex *vtx1 = editMap.vertexes[v1 - 1];
Vertex *vtx2 = editMap.vertexes[v2 - 1];
coord_t length = V2d_Distance(vtx2->origin(), vtx1->origin());
if(!(length > 0)) return 0;
if(!(V2d_Distance(vtx2->origin(), vtx1->origin()) > 0)) return 0;

SideDef *front = frontSide? editMap.sideDefs[frontSide - 1] : 0;
SideDef *back = backSide? editMap.sideDefs[backSide - 1] : 0;

LineDef *l = editMap.createLine();
l->_v1 = vtx1;
l->_v2 = vtx2;
Sector *frontSector = (frontSectorIdx == 0? NULL: editMap.sectors[frontSectorIdx-1]);
Sector *backSector = (backSectorIdx == 0? NULL: editMap.sectors[backSectorIdx-1]);

l->_v1->_buildData.refCount++;
l->_v2->_buildData.refCount++;

l->front()._sector = (frontSector == 0? NULL: editMap.sectors[frontSector-1]);
l->back()._sector = (backSector == 0? NULL: editMap.sectors[backSector-1]);
LineDef *l = editMap.createLine(*vtx1, *vtx2, frontSector, backSector);

l->front()._sideDef = front;
l->back()._sideDef = back;

l->_length = length;

l->updateSlopeType();
l->updateAABox();

l->_angle = bamsAtan2(int( l->_direction[VY] ),
int( l->_direction[VX] ));

// Remember the number of unique references.
if(l->hasFrontSideDef())
{
l->frontSideDef()._line = l;
l->frontSideDef()._buildData.refCount++;
}

l->back()._sideDef = back;
if(l->hasBackSideDef())
{
l->backSideDef()._line = l;
l->backSideDef()._buildData.refCount++;
}

l->_inFlags = 0;

// Determine the default linedef flags.
// Determine the default line flags.
l->_flags = flags;
if(!front || !back)
l->_flags |= DDLF_BLOCKING;

// Remember the number of unique references.
l->v1()._buildData.refCount++;
l->v2()._buildData.refCount++;

if(l->hasFrontSideDef())
{
l->frontSideDef()._buildData.refCount++;
}

if(l->hasBackSideDef())
{
l->backSideDef()._buildData.refCount++;
}

return l->origIndex();
}

Expand Down
18 changes: 9 additions & 9 deletions doomsday/client/src/map/bsp/partitioner.cpp
Expand Up @@ -228,39 +228,39 @@ DENG2_PIMPL(Partitioner)
bool isFront = false;
if(p.castHoriz)
{
if(de::abs(line.direction()[VY]) < DIST_EPSILON)
if(de::abs(line.direction().y) < DIST_EPSILON)
return;

if((de::max(line.v1Origin()[VY], line.v2Origin()[VY]) < p.mY - DIST_EPSILON) ||
(de::min(line.v1Origin()[VY], line.v2Origin()[VY]) > p.mY + DIST_EPSILON))
return;

dist = (line.v1Origin()[VX] + (p.mY - line.v1Origin()[VY]) * line.direction()[VX] / line.direction()[VY]) - p.mX;
dist = (line.v1Origin()[VX] + (p.mY - line.v1Origin()[VY]) * line.direction().x / line.direction().y) - p.mX;

isFront = ((p.testLine->direction()[VY] > 0) != (dist > 0));
isFront = ((p.testLine->direction().y > 0) != (dist > 0));
dist = de::abs(dist);

// Too close? (overlapping lines?)
if(dist < DIST_EPSILON)
return;

hitSector = line.sectorPtr((p.testLine->direction()[VY] > 0) ^ (line.direction()[VY] > 0) ^ !isFront);
hitSector = line.sectorPtr((p.testLine->direction().y > 0) ^ (line.direction().y > 0) ^ !isFront);
}
else // Cast vertically.
{
if(de::abs(line.direction()[VX]) < DIST_EPSILON)
if(de::abs(line.direction().x) < DIST_EPSILON)
return;

if((de::max(line.v1Origin()[VX], line.v2Origin()[VX]) < p.mX - DIST_EPSILON) ||
(de::min(line.v1Origin()[VX], line.v2Origin()[VX]) > p.mX + DIST_EPSILON))
return;

dist = (line.v1Origin()[VY] + (p.mX - line.v1Origin()[VX]) * line.direction()[VY] / line.direction()[VX]) - p.mY;
dist = (line.v1Origin()[VY] + (p.mX - line.v1Origin()[VX]) * line.direction().y / line.direction().x) - p.mY;

isFront = ((p.testLine->direction()[VX] > 0) == (dist > 0));
isFront = ((p.testLine->direction().x > 0) == (dist > 0));
dist = de::abs(dist);

hitSector = line.sectorPtr((p.testLine->direction()[VX] > 0) ^ (line.direction()[VX] > 0) ^ !isFront);
hitSector = line.sectorPtr((p.testLine->direction().x > 0) ^ (line.direction().x > 0) ^ !isFront);
}

// Too close? (overlapping lines?)
Expand Down Expand Up @@ -326,7 +326,7 @@ DENG2_PIMPL(Partitioner)
p.testLine = line;
p.mX = (line->v1Origin()[VX] + line->v2Origin()[VX]) / 2.0;
p.mY = (line->v1Origin()[VY] + line->v2Origin()[VY]) / 2.0;
p.castHoriz = (de::abs(line->direction()[VX]) < de::abs(line->direction()[VY])? true : false);
p.castHoriz = (de::abs(line->direction().x) < de::abs(line->direction().y)? true : false);

AABoxd scanRegion = map->bounds();
if(p.castHoriz)
Expand Down

0 comments on commit 3837717

Please sign in to comment.