I've loaded a 3d model using Helix toolking like this
modelGroupScull = importer.Load("C:\\Users\\Robert\\Desktop\\a.obj");
GeometryModel3D modelScull = (GeometryModel3D)modelGroupScull.Children[0];
and I also have _3DTools that can draw lines from point-to-point in 3d space. now to draw a wireframe of my GeometryModel3D I guess I have to cycle to its vertexes and add them to ScreenSpaceLines3D.
ScreenSpaceLines3D wireframe = new ScreenSpaceLines3D();
// need to cycle through all vertexes of modelScull as Points, to add them to wireframe
wireframe.Points.Add(new Point3D(1, 2, 3));
wireframe.Color = Colors.LightBlue;
wireframe.Thickness = 3;
Viewport3D1.Children.Add(wireframe);
But... how do I actually get this vertex points?
EDIT:
Thanks for the answer. It did add the points
ScreenSpaceLines3D wireframe = new ScreenSpaceLines3D();
MeshGeometry3D mg3 = (MeshGeometry3D)modelScull.Geometry;
foreach (Point3D point3D in mg3.Positions)
{
wireframe.Points.Add(point3D);
}
wireframe.Color = Colors.LightBlue;
wireframe.Thickness = 1;
Viewport3D1.Children.Add(wireframe);
but the wireframe is messed up )
(source: gyazo.com)
maybe someone knows of other ways to draw wireframes? )
Normally the trigons are drawn with index buffers (to prevent extra rotations of vertices) Take a look at the TriangleIndices:
if you do something like this: (not tested it)
MeshGeometry3D mg3 = (MeshGeometry3D)modelScull.Geometry;
for(int index=0;index<mg3.TriangleIndices.Count; index+=3)
{
ScreenSpaceLines3D wireframe = new ScreenSpaceLines3D();
wireframe.Points.Add(mg3.Positions[mg3.TriangleIndices[index]]);
wireframe.Points.Add(mg3.Positions[mg3.TriangleIndices[index+1]]);
wireframe.Points.Add(mg3.Positions[mg3.TriangleIndices[index+2]]);
wireframe.Points.Add(mg3.Positions[mg3.TriangleIndices[index]]);
wireframe.Color = Colors.LightBlue;
wireframe.Thickness = 1;
Viewport3D1.Children.Add(wireframe);
}
But, this can create some overdraw (2 lines on the same coordinates) and probably very slow.
If you put each side into a list and use something like a Distinct on it, it will be better.
The problem with the ScreenSpaceLines3D is that will continue the line, instead of create 1 line (start/end).
If you can manage a algoritm that tries to draw you model with 1 line, it will go faster.
Wireframes are very slow in WPF. (because they are created with trigons)
You should find the vertex points in MeshGeometry3D.Positions Property
foreach (var point3D in modelScull.Geometry.Positions)
Related
I have a set of geo coordinates as latitude/longitude pairs that I wish to project onto a 2D surface. Some of the coordinates are connected by lines forming a shape/outline/polygon.
I understand how to project individual points using one of the many available map projections like Mercator and then drawing them with Graphics.DrawArc but how do I go about projecting the connecting lines between them? I can't just project the two defining coordinates of a line and draw it Graphics.DrawLine because every single point on that line has to be projected as well, right? I don't know much about these things, so I hope you understand what I mean.
Is it even possible to do what I'm trying to do using just the methods provided by the System.Drawing.Graphics class? Can I do this with a projection matrix? If anyone could explain a bit how I would go about doing this, I would greatly appreciate it. Thanks.
If your are only drawing lines it is probably easiest just to subdivide the lines into short segments, project each segment point and draw straight lines between them. This might not be the most performant or exact way to do it. But it should be fairly easy to implement.
For example:
public static void DrawSubdivided(this Graphics g, Pen pen, Vector2 p1, Vector2 p2, float subdivisionLength)
{
var d = p2 - p1;
var length = d.Length();
// Add check for zero-length lines
var dn = d / length;
var points = new List<Vector2>();
for (float l = 0; l < length; l += subdivisionLength)
{
points.Add( p1 + dn * l);
}
points.Add(p2);
// apply transformation to the points
g.DrawLines(pen, points.Select(p => new PointF(p.X, p.Y)).ToArray());
}
This uses System.Numerics.Vector2 since it has reasonable arithmetic operations defined, in contrast to PointF.
I'm trying to get the corners of the following shape:
By corners I mean this (red dots):
The minimum quantity of points that can define this shape.
And I have implemented the following:
public Shape Optimize()
{
// If the vertices are null or empty this can't be executed
if (vertices.IsNullOrEmpty())
return this; // In this case, return the same instance.
if (!edges.IsNullOrEmpty())
edges = null; //Reset edges, because a recalculation was requested
// The corners available on each iteration
var corners = new Point[] { Point.upperLeft, Point.upperRight, Point.downLeft, Point.downRight };
//The idea is to know if any of the following or previous vertice is inside of the the array from upside, if it is true then we can add it.
Point[] vs = vertices.ToArray();
for (int i = 0; i < vertices.Count - 1; ++i)
{
Point backPos = i > 0 ? vs[i - 1] : vs[vertices.Count - 1],
curPos = vs[i], //Punto actual
nextPos = i < vertices.Count - 1 ? vs[i + 1] : vs[0];
// We get the difference point between the actual point and the back & next point
Point backDiff = backPos - curPos,
nextDiff = nextPos - curPos,
totalDiff = nextPos - backPos;
if (corners.Contains(backDiff) || corners.Contains(nextDiff) || corners.Contains(totalDiff))
AddEdge(curPos, center); // If any of the two points are defined in the corners of the point of before or after it means that the actual vertice is a edge/corner
}
return this;
}
This works rectangled shapes, but rotated shapes are very sharp, so, this code doesn't work well:
Blue pixels (in this photo and the following) are the vertices variable processed on Optimize method.
Green pixels are the detected corners/edges (on both photos).
But sharpness in a shape only defines the side inclination, so what can I do to improve this?
Also, I have tested Accord.NET BaseCornersDetector inherited classes, but the best result is obtained with HarrisCornersDetector, but:
Many edges/corners are innecesary, and they aren't in the needed place (see first photo).
Well, after hours of research I found a library called Simplify.NET that internally runs the Ramer–Douglas–Peucker algorithm.
Also, you maybe interested on the Bresenham algorithm, with this algorithm you can draw a line using two Points.
With this algorithm, you can check if your tolerance is too high, comparing the actual points and the points that this algorithm outputs and making some kind of percentage calculator of similarity.
Finally, is interesting to mention Concave Hull and Convex Hull algorithms.
All this is related to Unity3D.
My outputs:
And my implementation.
It's very important to say, that points needs to be sorted forcing them to be connected. If the shape is concave as you can see on the second photo maybe you need to iterate walls of the shape.
You can see an example of implementation here. Thanks to #Bunny83.
I have set of random generated points in 3D Scene, and in runtime I want to change the type of point markers to, for example, triangles, as in the picture:
Is it possible? How can I achieve this? Also I need change color for some points.
Scene initialization code below:
ILArray<float> points = ILMath.tosingle(ILMath.randn(3, 1000));
var scene = new ILScene
{
new ILPlotCube(twoDMode: false)
{
new ILPoints
{
Positions = points,
Color = null,
Size = 2
}
}
};
Markers (ILMarker) and 'points' (ILPoints) are very different beasts. Markers are much more flexible configurable, mostly look nicer and are much more expensive to render. They commonly consist out of a border (line shape) and a filled area (triangles shape) and come with a number of predefined looks.
ILPoints on the other hand are designed to be fast and easy. one can easily create millions of points without decreasing the plotting performance. Don't try this with markers! But such points are what they are: filled circles. It's it. No borders, no different shapes.
However, if you want to give it a try - even for the 1000 points in your questions - you can do so. Just use an ILLinePlot instead and configure a marker for it. You may set the line color to Color.Empty to have the markers showing up alone.
new ILLinePlot(points, lineColor: Color.Empty, markerStyle: MarkerStyle.TriangleDown)
In order to get individual colors for individual point markers you would split your markers (points) up into individual set of points. Create an ILLinePlot for each set of points using the scheme described above.
The part of your question dealing with 'dynamic' is also easy: You can change the type of markers as well as any other property at runtime. Here is one simple example which toogles the markers between red triangle markers and white rectangle markers by clicking anywhere on the scene:
ilPanel1.Scene.MouseClick += (_s, _a) => {
if (_a.DirectionUp) return;
var lp = ilPanel1.Scene.First<ILLinePlot>();
if (lp != null) {
if (lp.Marker.Style == MarkerStyle.TriangleDown) {
lp.Marker.Style = MarkerStyle.Rectangle;
lp.Marker.Fill.Color = Color.White;
} else {
lp.Marker.Style = MarkerStyle.TriangleDown;
lp.Marker.Fill.Color = Color.Red;
}
lp.Configure();
ilPanel1.Refresh();
}
};
I have a project with a graphic object called GraphicsLine. What it does is simply draw line on every mousedown and stop on every mouseup, nothing complicated. It stores coordinates for the start and the end of the line (x,y). Now what I want to know is whenever a shape is created. For example, you create 4 lines that forms a square, I want to be able to run an algorithm that can tell me that there is a square in the drawing.
Note that the shape can be anything that is "closed". Not only square, rectangle or triangle.
The goal of this is to calculate the area of the created shapes.
Is there something that already exists for doing this? I've been struggling to find something that could fit my needs.
EDIT 1:
I added some additionnal information :
Lines are either "cliped" to another line start or end point or they are not. There is no close closure, it is on the same point or not closed at all. 1 line can be used in multiple shapes.
EDIT 2 :
So basically, I want something that can give me an array of "GraphicsLine" that forms a shape. So if we have 6 lines in the drawing but 4 of them forms a square, I want something that returns those 4 lines so I can create another object from it.
Thanks in advance.
Please check this question How do I calculate the area of a 2d polygon? it is probably what you need, you just have to port it to C# :)
Edit: from #chmike answer:
Where x and y are the arrays of coordinates
var x = [10,10,20,20];
var y = [10,20,20,10];
var n = x.Length;
x[n] = x[0];
x[n+1] = x[1];
y[n] = y[0];
y[n+1] = y[1];
// compute area
int area = 0;
for(var i = 1; i <= n; ++i ) {
area += x[i]*( y[i+1] - y[i-1] );
}
Console.Write(area /= 2);
Take a look at this tutorial of AForge.NET Shape Checker
If your GraphicsLine object is drawing on a PictureBox or if you can convert that object to a bitmap, you can then run the code below.
You can easily try detection of quadrilaterals, this code here will actually highlight the detected objects, you do change the loop and make it do whatever you want instead:
// if you are using a PictureBox to draw the shapes, then convert it to a bitmap
Bitmap bitmap = new Bitmap(pictureBox1.Image)
// locate objects using blob counter
BlobCounter blobCounter = new BlobCounter( );
blobCounter.ProcessImage( bitmap );
Blob[] blobs = blobCounter.GetObjectsInformation( );
// create Graphics object to draw on the image and a pen
Graphics g = Graphics.FromImage( bitmap );
Pen bluePen = new Pen( Color.Blue, 2 );
// check each object and draw circle around objects, which are recognized as circles
for ( int i = 0, n = blobs.Length; i < n; i++ )
{
List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints( blobs[i] );
List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners( edgePoints );
g.DrawPolygon( bluePen, ToPointsArray( corners ) );
}
bluePen.Dispose( );
g.Dispose( );
I know it is 2 years + later, but I found a way with a recursive function to know when the shapes are "closed". You start from any points of the last drawed line, then you check if the other point is connected to another line. You do this until you reach the starting point. I save all the lines into another class called Polygon. This way, they keep all the lines with the start and finish that forms the polygon. Then to calculate the area, I do as Eduardo Cobuci said in his answer.
Hope this helps.
I'm doing a Revit Macro to get the center point of a part (floor part) to check if it is inside a room or a space.
I couldn't get much of the BoundingBox object which is giving me a point outside the part, so I tried to use the Geometry element internal faces getting the mesh vertices but I'm stuck calculating the mid point.
I'm using a rather naive algorithm shown in the snippet below, but it's giving me false results as it seems to be affected by the initial default of min/max variables.
Any suggestions?
PS: DebugTools is a custom helper class of my own.
public void ZoneDetect()
{
Document doc = this.ActiveUIDocument.Document;
using (Transaction t = new Transaction(doc,"Set Rooms By Region"))
{
t.Start();
FilteredElementCollector fec =
new FilteredElementCollector(doc)
.OfClass(typeof(Part))
.OfCategory(BuiltInCategory.OST_Parts)
.Cast<Part>();
foreach (Part p in fec)
{
Options op = new Options();
op.View=doc.ActiveView;
op.ComputeReferences=true;
GeometryElement gm=p.get_Geometry(op);
Solid so = gm.First() as Solid;
PlanarFace fc=so.Faces.get_Item(0) as PlanarFace;
foreach (PlanarFace f in so.Faces)
{
if (f.Normal == new XYZ(0,0,-1)) fc=f;
}
XYZ max = new XYZ();
XYZ min = new XYZ();
int no = 0;
foreach (XYZ vx in fc.Triangulate().Vertices)
{
// Just for debugging
DebugTools.DrawModelTick(vx,doc,"Max");
doc.Regenerate();
TaskDialog.Show("Point:"+no.ToString(),vx.ToString());
no++;
//Comparing points
if (vx.X>max.X) max=new XYZ (vx.X,max.Y,0);
if (vx.Y>max.Y) max=new XYZ (max.X,vx.Y,0);
if (vx.X<min.X) min=new XYZ (vx.X,min.Y,0);
if (vx.Y<min.Y) min=new XYZ (min.X,vx.Y,0);
}
XYZ mid = new XYZ(max.X-min.X,max.Y-min.Y,0);
DebugTools.DrawModelTick(mid,doc,"Mid");
DebugTools.DrawModelTick(max,doc,"Max");
DebugTools.DrawModelTick(min,doc,"Min");
}
t.Commit();
}
}
It seems like you're looking for the center of gravity of a polygon. An algorithm for that can be found here: Center of gravity of a polygon
Once you have a Face object, you can enumerate its edges to receive a list of vertex points. Use the longest of the EdgeLoops in the face. Collect all the points and make sure that they are in the right order (the start and end points of the edges might need to be swapped).
Daren & Matt thanks a lot for your answers,
Since I'm dealing with rather simple shapes ( mainly rectangles ) I just needed to get a point roughly near the center to test whether it is inside a room, my problem was with the naive algorithm I was using which turned out to be wrong.
I corrected it as follows:
XYZ midSum = Max + Min;
XYZ mid = new XYZ(midSum.X/2 , midSum.Y/2,0);
I will look into refining it using the link you've provided, but as for now I will get into finishing my task in hand.
Many thanks