I have a weird issues with BufferGeometryUtils.mergeVertices method
I took a simple cube from a jsfiddle and performed the mergeVertices and it seems mergeVertices does not merge all identical vertices as it should. See https://jsfiddle.net/tomfree/vpdwmycn/
E.g. the vertices given in the indexGeometry returned by mergeVertices are
Index 0 = (-1, -1, 1, )
index 1 = (1, -1, 1,)
index 2= (-1, 1, 1, )
index 3= (1, 1, 1,)
index 4= (1, -1, 1)
i.e. vertices at position 1 and 4 seem to be the same but shouldn't after mergeVertices to my understanding. I am pretty sure that I am missing sth. Can someone point me into the right direction?
Cheers
Tom
ps: Also posted as https://discourse.threejs.org/t/buffergeometryutils-mergevertices-seems-not-to-merge-all-identical-vertices/33890 in three.js forum
The merge process is functioning as expected. If you look at the count of each attribute of the original geometry you'll get 36, but if you look at the count of each attribute of the merged indexedGeo, you'll get 24:
console.log(geometry.getAttribute("position").count); // 36
console.log(indexedGeo.getAttribute("position").count); // 24
The problem you're encountering is that the merge method looks at all attributes before merging. In your case, position, normal, uv. The corners of a box have 3 vertices, each with its own normal pointing in 3 directions (one pointing up, one right, and one forward, for example). Since each vertex contains unique normal data on how to render its respective face, these vertices cannot be merged. Even though their position is the same, the other attributes are different.
Related
I have a mesh, with gradient color using this type of code :
It's nice and beautiful, but I want to reduce the precision of the gradient and make it less smooth.
Here's an exemple
I've got data on a JSON, wich gave me coordinate for vertices 0, 2, 4 and 6. I calculate the other one after that. I've got a value on vertice 0, 2, 4 and 6, which I use to get the color value of that point, in HSL (like 0 is 0 in HSL and 1 is 240 in HSL)
With than given value, 1, 3, 5 and 7 have a color value depending of the vertice on the same line, and 8 got value from a pondered calculus.
If 0, 6 have a value of 0.5(green), and 2, 4 have a value of 1(red), then 7 is green, 3 is red, and 1, 8, 5 have a value of 0.75 (yellow).
With my material and colorVertex, the pixels between those point are calculated and can take a infity of value between 1 and 0.5.
Now, I want to now if it's possible to limit this infinity of values to fixed one, so it will look like that
I can't subdivise my mesh because the final one is really big and can't spend much more on calculus time. Is there a way to change the interpolation used by three.js so the pixel between my vertices have the colormap/color range that I want?
Thanks in advance
I am searching for a library which can do the decomposition of polygons. I want define define directions or lines in which the polygon should be fragmented, as seen here:
So that I get the small polygons. Anyone know a library which supports this?
Or any ideas?
I'm not sure which language are you using. I have a library, written for my purposes, that can get a full partition by given line set and return polygons as a result. It is written on PHP and called dimension and, using it, you can solve your question like way:
Define your polygon by a set of lines LineSet_2D or a Polygon_2D
Define partition lines also through Line_2D
Use LineSet_2D method getPolygons to find all polygons
I've written an example:
//define or polygon. Note that Polygon_2D can also be used
$rPolygon = new LineSet_2D(
new Line_2D( 0, 3, 1, 1),
new Line_2D( 1, 1, 3, 0),
new Line_2D( 3, 0, 1,-1),
new Line_2D( 1,-1, 0,-3),
new Line_2D( 0,-3,-1,-1),
new Line_2D(-1,-1,-3,0),
new Line_2D(-3, 0,-1, 1),
new Line_2D(-1, 1, 0, 3)
);
//define partition line set
$rPartition = new LineSet_2D(
new Line_2D(-1, 1, 1,-1),
new Line_2D(-1,-1, 1, 1)
);
//result line set:
$rResultSet = LineSet_2D::createFromArray(array_merge(
$rPolygon->getLines(),
$rPartition->getLines()
));
//for example, dump plain result:
var_dump($rResultSet->getPolygons());
You can also find this example here But I think it is not exact solution for your question, since my LineSet_2D class will return all looped polygons (i.e. not only 'pieces').
You are looking for a "polygon chop" boolean operation. You may google it for resources available.
Some queues on doing this on your own..
For each slicing line..
Find the intersection points of the slicing line with the edges of the polygon.
For each edge that it intersects, split the edge into two.
Split the polygon corresponding to the splitted edge, into two polygons.
Do the same for all polygons.
You will have to take care of special cases, like splitting line going through vertex and so on...
First I am not sure which keywords to use for this and I think I am probably using the wrong ones to google about it, so if someone could give me any hint it would be much appreciated.
My problem is the following:
I need to find the "rooms" inside a house plan. For example take this geometry:
The desired algorithm would tell me which vertexes bound each of the rooms. So for this example it would be:
room A: 1, 2, 9, 10, 3, 4, 5, 8 ,1
room B: 2, 3, 10, 9, 2
room C: 11, 12, 14, 13, 11
room D: 5, 6, 7, 8, 5
I have the vertexes and the edges as input data.
Edit:
The edge data is as follows (edge 8, 1 ,2):
x y
47 196
47 85
258 85
it is in pixel coord.
Graph Theory did not really help me because I have disconnected loops that share information. For example [1 2 9 10 3 4 5 8 1] AND [11 12 14 13 11]. So in the end I ended up doing a image fill, when expanding the boarders of the fill 1 pixel and doing a boolen operation to figure out which vertex are inside the filled image.
This is planar graph. It has V vertices, E edges and F = E - V + 2 faces (including outer face). We have to determine the edge list for all the faces. Every edge will be used twice in these lists (in forward and backward direction).
Create main arc list, add all the arcs (i.e. for 1-2 undirected edge add both 1-2 and 2-1 directed arcs)
Find the lowest vertex point. If there are some such points, choose the leftmost one (7th here).
Travel the outer face (contour) in CCW direction (choose the rightmost outgoing arc at every vertex): 7-6-5-4-3-2-1-7. Remove visited arcs from the main list.
Get any arc from the main list, travel the first inner face, follow the right-hand rule (i.e. 7-8-5-6-7), remove visited arcs.
Repeat until the main list is empty.
Repeat all the procedure for disconnected components (11-12-13-14)
One of possible solutions is to triangulate this area so that every input edge is an edge of some triangle. Then split triangles into connected sets and find their border.
There are several algorithms for triangulation: ear-clipping, Delaunay, ...
I have an X by Y space, where X and Y are determined by the sizes of the rectangles given to me. I'm inserting oriented rectangles of a certain size into the space, one at a time. Each insertion is as far left as possible, and then as far up as possible (so as close to (0,0) as I can). What's the best way to represent this? I'm implementing this in Python. The top answer to this question is helpful, but I was hoping for some Python-specific advice, as I'm pretty new to the language itself as well.
Thanks!
If you are trying to efficiently pack rectangles, there are some established algorithms. Here's a Python implementation of one particular algorithm. There's also an article on packing lightmaps here for which I have a Python version (I honestly don't remember whether I ported it myself, or got it from somewhere else).
You have two choices for working in two-dimensional space like this.
A list of lists. [ [0, 0, ..., 0], [0, 0, ..., 0], ... [0, 0, ..., 0] ] The outer list is the 'X' access, the inner list is the 'Y' access. Each point is space[x][y]. You build it with space = list( list( EMPTY for j in range(Y_size) ) for i in range(X_size) ) or something similar.
You mask off rectangles with some filler algorithm that sets values into a rectangular patch of space.
for x in range( low, high ):
for y in range ( low, high ):
space[x][y]= FILLED # or whatever object you're putting there.
A mapping. { (0,0): 0, (0,1): 0, ... (X,Y): 0 }. Each point is space[x,y]. You build it with space = dict( ( (x,y), EMPTY ) for x in range(X_size) for y in range(Y_size) ).
You mask off rectangles with almost the same filler algorithm. Just change the syntax slightly.
Quadtrees are often used for this sort of thing. It sounds like you want a region quad tree.
For this problem speed is pretty crucial. I've drawn a nice image to explain the problem better. The algorithm needs to calculate if edges of a rectangle continue within the confines of the canvas, will the edge intersect another rectangle?
We know:
The size of the canvas
The size of each rectangle
The position of each rectangle
The faster the solution is the better! I'm pretty stuck on this one and don't really know where to start.
alt text http://www.freeimagehosting.net/uploads/8a457f2925.gif
Cheers
Just create the set of intervals for each of the X and the Y axis. Then for each new rectangle, see if there are intersecting intervals in the X or the Y axis. See here for one way of implementing the interval sets.
In your first example, the interval set on the horizontal axis would be { [0-8], [0-8], [9-10] }, and on the vertical: { [0-3], [4-6], [0-4] }
This is only a sketch, I abstracted many details here (e.g. usually one would ask an interval set/tree "which intervals overlap this one", instead of "intersect this one", but nothing not doable).
Edit
Please watch this related MIT lecture (it's a bit long, but absolutely worths it).
Even if you find simpler solutions (than implementing an augmented red-black tree), it's good to know the ideas behind these things.
Lines that are not parallel to each other are going to intersect at some point. Calculate the slopes of each line and then determine what lines they won't intersect with.
Start with that, and then let's see how to optimize it. I'm not sure how your data is represented and I can't see your image.
Using slopes is a simple equality check which probably means you can take advantage of sorting the data. In fact, you can probably just create a set of distinct slopes. You'll have to figure out how to represent the data such that the two slopes of the same rectangle are not counted as intersecting.
EDIT: Wait.. how can two rectangles whose edges go to infinity not intersect? Rectangles are basically two lines that are perpendicular to each other. shouldn't that mean it always intersects with another if those lines are extended to infinity?
as long as you didn't mention the language you chose to solve the problem, i will use some kind of pseudo code
the idea is that if everything is ok, then a sorted collection of rectangle edges along one axis should be a sequence of non-overlapping intervals.
number all your rectangles, assigning them individual ids
create an empty binary tree collection (btc). this collection should have a method to insert an integer node with info btc::insert(key, value)
for all rectangles, do:
foreach rect in rects do
btc.insert(rect.top, rect.id)
btc.insert(rect.bottom, rect.id)
now iterate through the btc (this will give you a sorted order)
btc_item = btc.first()
do
id = btc_item.id
btc_item = btc.next()
if(id != btc_item.id)
then report_invalid_placement(id, btc_item.id)
btc_item = btc.next()
while btc_item is valid
5,7,8 - repeat steps 2,3,4 for rect.left and rect.right coordinates
I like this question. Here is my try to get on it:
If possible:
Create a polygon from each rectangle. Treat each edge as an line of maximum length that must be clipped. Use a clipping algorithm to check weather or not a line intersects with another. For example this one: Line Clipping
But keep in mind: If you find an intersection which is at the vertex position, its a valid one.
Here's an idea. Instead of creating each rectangle with (x, y, width, height), instantiate them with (x1, y1, x2, y2), or at least have it interpret these values given the width and height.
That way, you can check which rectangles have a similar x or y value and make sure the corresponding rectangle has the same secondary value.
Example:
The rectangles you have given have the following values:
Square 1: [0, 0, 8, 3]
Square 3: [0, 4, 8, 6]
Square 4: [9, 0, 10, 4]
First, we compare Square 1 to Square 3 (no collision):
Compare the x values
[0, 8] to [0, 8] These are exactly the same, so there's no crossover.
Compare the y values
[0, 4] to [3, 6] None of these numbers are similar, so they're not a factor
Next, we compare Square 3 to Square 4 (collision):
Compare the x values
[0, 8] to [9, 10] None of these numbers are similar, so they're not a factor
Compare the y values
[4, 6] to [0, 4] The rectangles have the number 4 in common, but 0 != 6, therefore, there is a collision
By know we know that a collision will occur, so the method will end, but lets evaluate Square 1 and Square 4 for some extra clarity.
Compare the x values
[0, 8] to [9, 10] None of these numbers are similar, so they're not a factor
Compare the y values
[0, 3] to [0, 4] The rectangles have the number 0 in common, but 3 != 4, therefore, there is a collision
Let me know if you need any extra details :)
Heh, taking the overlapping intervals answer to the extreme, you simply determine all distinct intervals along the x and y axis. For each cutting line, do an upper bound search along the axis it will cut based on the interval's starting value. If you don't find an interval or the interval does not intersect the line, then it's a valid line.
The slightly tricky part is to realize that valid cutting lines will not intersect a rectangle's bounds along an axis, so you can combine overlapping intervals into a single interval. You end up with a simple sorted array (which you fill in O(n) time) and a O(log n) search for each cutting line.