renamed SectorTools to Tools and removed useless Polygon class (methods are in Tools now)

This commit is contained in:
codeimp 2008-10-06 19:33:13 +00:00
parent c678e615a2
commit 2dd337cffa
7 changed files with 80 additions and 171 deletions

View file

@ -116,8 +116,7 @@
<Compile Include="Geometry\Angle2D.cs" />
<Compile Include="Geometry\LinedefsTracePath.cs" />
<Compile Include="Geometry\LinedefAngleSorter.cs" />
<Compile Include="Geometry\Polygon.cs" />
<Compile Include="Geometry\SectorTools.cs" />
<Compile Include="Geometry\Tools.cs" />
<Compile Include="Geometry\Triangulator.cs" />
<Compile Include="Geometry\EarClipVertex.cs" />
<Compile Include="Geometry\Line2D.cs" />

View file

@ -488,14 +488,14 @@ namespace CodeImp.DoomBuilder.BuilderModes
{
// Find the shortest, closest path between these lines
List<List<LinedefSide>> paths = new List<List<LinedefSide>>(8);
paths.Add(SectorTools.FindClosestPath(l1, true, l2, true, true));
paths.Add(SectorTools.FindClosestPath(l1, true, l2, false, true));
paths.Add(SectorTools.FindClosestPath(l1, false, l2, true, true));
paths.Add(SectorTools.FindClosestPath(l1, false, l2, false, true));
paths.Add(SectorTools.FindClosestPath(l2, true, l1, true, true));
paths.Add(SectorTools.FindClosestPath(l2, true, l1, false, true));
paths.Add(SectorTools.FindClosestPath(l2, false, l1, true, true));
paths.Add(SectorTools.FindClosestPath(l2, false, l1, false, true));
paths.Add(Tools.FindClosestPath(l1, true, l2, true, true));
paths.Add(Tools.FindClosestPath(l1, true, l2, false, true));
paths.Add(Tools.FindClosestPath(l1, false, l2, true, true));
paths.Add(Tools.FindClosestPath(l1, false, l2, false, true));
paths.Add(Tools.FindClosestPath(l2, true, l1, true, true));
paths.Add(Tools.FindClosestPath(l2, true, l1, false, true));
paths.Add(Tools.FindClosestPath(l2, false, l1, true, true));
paths.Add(Tools.FindClosestPath(l2, false, l1, false, true));
foreach(List<LinedefSide> p in paths)
if((p != null) && ((shortestpath == null) || (p.Count < shortestpath.Count))) shortestpath = p;
@ -591,7 +591,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
foreach(Linedef ld in newlines)
{
// Find closest path starting with the front of this linedef
List<LinedefSide> pathlines = SectorTools.FindClosestPath(ld, true, true);
List<LinedefSide> pathlines = Tools.FindClosestPath(ld, true, true);
if(pathlines != null)
{
// Make polygon
@ -606,7 +606,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
// flip the line if it is not helping.
// Find closest path starting with the back of this linedef
pathlines = SectorTools.FindClosestPath(ld, false, true);
pathlines = Tools.FindClosestPath(ld, false, true);
if(pathlines != null)
{
// Make polygon
@ -658,13 +658,13 @@ namespace CodeImp.DoomBuilder.BuilderModes
if(!frontsdone[i])
{
// Find a way to create a sector here
List<LinedefSide> sectorlines = SectorTools.FindPotentialSectorAt(ld, true);
List<LinedefSide> sectorlines = Tools.FindPotentialSectorAt(ld, true);
if(sectorlines != null)
{
sidescreated = true;
// Make the new sector
Sector newsector = SectorTools.MakeSector(sectorlines);
Sector newsector = Tools.MakeSector(sectorlines);
// Go for all sidedefs in this new sector
foreach(Sidedef sd in newsector.Sidedefs)
@ -690,7 +690,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
if(!backsdone[i])
{
// Find a way to create a sector here
List<LinedefSide> sectorlines = SectorTools.FindPotentialSectorAt(ld, false);
List<LinedefSide> sectorlines = Tools.FindPotentialSectorAt(ld, false);
if(sectorlines != null)
{
// We don't always want to create a new sector on the back sides
@ -716,7 +716,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
sidescreated = true;
// Join the new sector
Sector newsector = SectorTools.JoinSector(sectorlines, joinsidedef);
Sector newsector = Tools.JoinSector(sectorlines, joinsidedef);
// Go for all sidedefs in this new sector
foreach(Sidedef sd in newsector.Sidedefs)

View file

@ -432,10 +432,6 @@ namespace CodeImp.DoomBuilder.BuilderModes
// This checks and returns the grip the mouse pointer is in
private Grip CheckMouseGrip()
{
// Make polygon from corners
Polygon rectpoly = new Polygon();
rectpoly.AddRange(corners);
if(PointInRectF(resizegrips[0], mousemappos))
return Grip.SizeN;
else if(PointInRectF(resizegrips[2], mousemappos))
@ -452,7 +448,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
return Grip.RotateRB;
else if(PointInRectF(rotategrips[3], mousemappos))
return Grip.RotateLB;
else if(rectpoly.Intersect(mousemappos))
else if(Tools.PointInPolygon(corners, mousemappos))
return Grip.Main;
else
return Grip.None;

View file

@ -154,7 +154,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
// Find new sector
General.Interface.SetCursor(Cursors.AppStarting);
nearestside = newnearest;
allsides = SectorTools.FindPotentialSectorAt(mousemappos);
allsides = Tools.FindPotentialSectorAt(mousemappos);
if(allsides != null)
{
alllines = new List<Linedef>(allsides.Count);
@ -289,7 +289,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
General.Interface.SetCursor(Cursors.WaitCursor);
General.Settings.FindDefaultDrawSettings();
General.Map.UndoRedo.CreateUndo("Make Sector", UndoGroup.None, 0);
Sector s = SectorTools.MakeSector(allsides);
Sector s = Tools.MakeSector(allsides);
General.Interface.SetCursor(Cursors.Default);
// Quickly flash this sector to indicate it was created
@ -334,7 +334,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
General.Interface.SetCursor(Cursors.WaitCursor);
General.Settings.FindDefaultDrawSettings();
General.Map.UndoRedo.CreateUndo("Make Sector", UndoGroup.None, 0);
Sector s = SectorTools.MakeSector(allsides);
Sector s = Tools.MakeSector(allsides);
General.Interface.SetCursor(Cursors.Default);
// Edit the sector

View file

@ -56,6 +56,9 @@ namespace CodeImp.DoomBuilder.BuilderModes
protected Sector highlighted;
private Association highlightasso = new Association();
// TEST:
private Vector2D labelpos;
// Interface
protected bool editpressed;
@ -151,6 +154,12 @@ namespace CodeImp.DoomBuilder.BuilderModes
// New association highlights something?
if((s != null) && (s.Tag > 0)) completeredraw = true;
// TEST:
if(s != null)
{
labelpos = Tools.FindLabelPosition(s, 30.0f / renderer.Scale);
}
// If we're changing associations, then we
// need to redraw the entire display
if(completeredraw)
@ -191,8 +200,15 @@ namespace CodeImp.DoomBuilder.BuilderModes
// Done
renderer.Finish();
renderer.Present();
}
// TEST:
if(renderer.StartOverlay(true))
{
renderer.RenderRectangleFilled(new RectangleF(labelpos.x - 5, labelpos.y - 5, 10, 10), General.Colors.Indication, true);
renderer.Finish();
}
renderer.Present();
}
// Show highlight info
@ -337,6 +353,7 @@ namespace CodeImp.DoomBuilder.BuilderModes
{
if((highlighted != null) && !highlighted.IsDisposed) BuilderPlug.Me.RenderReverseAssociations(renderer, highlightasso);
if(selecting) RenderMultiSelection();
renderer.RenderRectangleFilled(new RectangleF(labelpos.x - 5, labelpos.y - 5, 10, 10), General.Colors.Indication, true);
renderer.Finish();
}

View file

@ -1,101 +0,0 @@
#region ================== Copyright (c) 2007 Pascal vd Heiden
/*
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#endregion
#region ================== Namespaces
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using CodeImp.DoomBuilder.Geometry;
using CodeImp.DoomBuilder.Rendering;
using SlimDX.Direct3D9;
using System.Drawing;
using CodeImp.DoomBuilder.Map;
#endregion
namespace CodeImp.DoomBuilder.Geometry
{
public class Polygon : List<Vector2D>
{
#region ================== Variables
#endregion
#region ================== Properties
#endregion
#region ================== Constructors
// Constructor
public Polygon()
{
// Initialize
}
#endregion
#region ================== Methods
// Point inside the polygon?
// See: http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
public bool Intersect(Vector2D p)
{
float miny, maxy, maxx, xint;
Vector2D v1 = base[base.Count - 1];
Vector2D v2;
int index = 0;
uint c = 0;
// Go for all vertices
while(index < base.Count)
{
// Get next vertex
v2 = base[index];
// Determine min/max values
miny = Math.Min(v1.y, v2.y);
maxy = Math.Max(v1.y, v2.y);
maxx = Math.Max(v1.x, v2.x);
// Check for intersection
if((p.y > miny) && (p.y <= maxy))
{
if(p.x <= maxx)
{
if(v1.y != v2.y)
{
xint = (p.y - v1.y) * (v2.x - v1.x) / (v2.y - v1.y) + v1.x;
if((v1.x == v2.x) || (p.x <= xint)) c++;
}
}
}
// Move to next
v1 = v2;
index++;
}
// Inside this polygon?
return (c & 0x00000001UL) != 0;
}
#endregion
}
}

View file

@ -33,10 +33,9 @@ using CodeImp.DoomBuilder.IO;
namespace CodeImp.DoomBuilder.Geometry
{
/// <summary>
/// This makes a sector from all surrounding lines from a given coordinate.
/// Automatically finds the sidedef/sector properties from surrounding sectors/sidedefs.
/// Tools to work with geometry.
/// </summary>
public static class SectorTools
public static class Tools
{
#region ================== Structures
@ -53,6 +52,46 @@ namespace CodeImp.DoomBuilder.Geometry
#endregion
#region ================== Polygons and Triangles
// Point inside the polygon?
// See: http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
public static bool PointInPolygon(ICollection<Vector2D> polygon, Vector2D point)
{
Vector2D v1 = General.GetByIndex<Vector2D>(polygon, polygon.Count - 1);
uint c = 0;
// Go for all vertices
foreach(Vector2D v2 in polygon)
{
// Determine min/max values
float miny = Math.Min(v1.y, v2.y);
float maxy = Math.Max(v1.y, v2.y);
float maxx = Math.Max(v1.x, v2.x);
// Check for intersection
if((point.y > miny) && (point.y <= maxy))
{
if(point.x <= maxx)
{
if(v1.y != v2.y)
{
float xint = (point.y - v1.y) * (v2.x - v1.x) / (v2.y - v1.y) + v1.x;
if((v1.x == v2.x) || (point.x <= xint)) c++;
}
}
}
// Move to next
v1 = v2;
}
// Inside this polygon?
return (c & 0x00000001UL) != 0;
}
#endregion
#region ================== Pathfinding
/// <summary>
@ -588,47 +627,6 @@ namespace CodeImp.DoomBuilder.Geometry
Vector2D foundpoint = new Vector2D();
float founddist = 0.0f;
// Calculate grid numbers
RectangleF bbox = s.CreateBBox();
int nodesx = (int)(bbox.Width / resolution);
int nodesy = (int)(bbox.Height / resolution);
// Make list of linedefs
List<Linedef> lines = new List<Linedef>(s.Sidedefs.Count);
foreach(Sidedef sd in s.Sidedefs) lines.Add(sd.Line);
// Scan the polygon
for(int x = 0; x < nodesx; x++)
{
for(int y = 0; y < nodesy; y++)
{
// Calculate absolute point
Vector2D p = new Vector2D(bbox.X + (float)x * resolution,
bbox.Y + (float)y * resolution);
// Point inside sector?
Linedef ld = MapSet.NearestLinedef(lines, p);
Sidedef sd = (ld.SideOfLine(p) < 0) ? ld.Front : ld.Back;
if((sd != null) && (sd.Sector == s))
{
// Go for all linedefs to calculate the smallest distance
float mindist = int.MaxValue;
foreach(Linedef l in lines)
{
float dist = l.DistanceToSq(p, true);
if(dist < mindist) mindist = dist;
}
// Better match?
if(mindist > founddist)
{
foundpoint = p;
founddist = mindist;
}
}
}
}
return foundpoint;
}