Skip to content

Commit

Permalink
Intersect Edges Mk3 (#4144)
Browse files Browse the repository at this point in the history
* edges intersectect numpy alg_1

* docs images and table fixes

* table fix 2

* mk3 version 3d algorithm and more
  • Loading branch information
vicdoval committed Jun 22, 2021
1 parent 5bc1be6 commit a5f8693
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 119 deletions.
25 changes: 25 additions & 0 deletions data_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
float64,
int32, int64)
from sverchok.utils.logging import info, debug
import numpy as np


DEBUG_MODE = False
Expand Down Expand Up @@ -1099,6 +1100,30 @@ def has_element(pol_edge):
return True
return False


def cross_indices_np(n):
'''
create list with all the indices pairs
for n=3 outputs a numpy array with:
[0,1]
[0,2]
[1,2]
'''

nu = np.sum(np.arange(n, dtype=np.int64))
ind = np.zeros((nu, 2), dtype=np.int16)
c = 0
for i in range(n-1):
l = n-i-1
np_i = np.full(n-i-1, i, dtype=np.int32)
np_j = np.arange(i+1, n, dtype=np.int32)
np_a = np.stack((np_i, np_j), axis=-1)
ind[c:c+l, :] = np_a
c += l

return ind

def no_space(s):
return s.replace(' ', '_')

Expand Down
1 change: 1 addition & 0 deletions docs/nodes/CAD/CAD_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CAD
:maxdepth: 2

crop_mesh_2d
edges_intersect_mk3
edges_to_faces_2d
merge_mesh_2d
merge_mesh_2d_lite
Expand Down
128 changes: 128 additions & 0 deletions docs/nodes/CAD/edges_intersect_mk3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
Intersect Edges
===============

.. image:: https://user-images.githubusercontent.com/28003269/70894030-a7c74080-2005-11ea-8594-dced40b05c03.png

Functionality
-------------

The node is designed for finding intersections of inputs edges and returns mesh based on intersection result.

This node contains several algorithm of finding edge intersections:

**3D algorithm:**

This is a brute force algorithm written in NumPy, it is pretty fast and pretty consistent but can produce double points that can be
removed with the remove doubles toggle. It ignores overlapping edges.

**2D algorithms**

**Alg_1**

Brute force algorithm that exposes every pair of edges to a mathutils function called 'intersect_line_line'. Does not check
if there are repeated points. It ignores overlapping edges

**Np**

This is a brute force algorithm written in NumPy, it is pretty fast and pretty consistent but can produce double points that can be
removed with the remove doubles toogle. It ignores overlapping edges

**Sweep line algorithm**

This algorithm is based on `sweep line <https://en.wikipedia.org/wiki/Sweep_line_algorithm>`_ approach:

It can handle big number of edges but if there are too much intersection the performance can slow down dramatically.
For example if each input edge intersect with every others. In this case only few edges can be handled by this node.

- The algorithm does not produce double points.
- It is less robust and can give an error.
- For this algorithm epsilon parameter on N panel is more important. If you get an error you can try to change the parameter to fix.

Prefix 2D means that the node expects from input any kind of flatten mesh
but it does not mean that the mesh should only lay on XY surface.
Input mesh can below or above XY surface or even can be tilted relative one.

The algorithm of finding new intersection is like this:

It takes two edges, removes Z coordinates and finds their intersection.
But after intersection is found it projects intersection point back onto one of the given edges.
This means that input mesh still should be flat but it can be located in space however you like.
Only location of input flatten mesh along Z coordinate will bring the error.

**Blender mode**

This mode is using internal Blender function from `mathutils` module.
It is pretty fast with not big number of intersections (about 1000)
but is quite unstable and can cause crash of Blender easily.
Also it is not design for such cases where one edge is intersect with all or most of all other edges. Use with care.
It's available only with Blender 2.81+


Benchmark of 2D algorithms
--------------------------

Three different test to determine the efficiency of the algorithms.
- Random Segments: Segments between random vectors
- Star Segments: Rotated segments from a center point (every segments intersects with the rest of segments in the center)
- Spine Segments: Parallel Segments intersected with a middle segment that cuts them in the center.

.. image:: https://user-images.githubusercontent.com/10011941/120835106-30bc9980-c564-11eb-93c5-bdbabd1a63fe.png

+-----------+-----------------------------------+-----------------------------+-----------------------------+
|Test | Random Segments | Star segments | Spine Segments |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Edges In | 100 | 1000 | 10000 | 100 | 1000 | 10000 | 100 | 1000 | 10000 |
|Intersec. | 1299 | 127892 | 12355630 | 1 | 1 | 1 | 100 | 1000 | 10000 |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Alg_1 | 6 ms | 694 ms | 74872 ms | 15 ms | 2125 ms | SC* | 3 ms | 226 ms | 22618 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Alg_1 + RD*| 10 ms | 1477 ms | 226240 ms | 25 ms | 3300 ms | SC* | 4 ms | 246 ms | 22626 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Sweep Line | 900 ms | 76700 ms | SC* | 53 ms | 778ms | 2000 ms | 81 ms | 1200 ms | 15500 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Blender | 3 ms | 650 ms | 112240 ms/BC* | 0.6 ms | 20ms | 10643 ms | 0.8 ms | 12 ms | 1100 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Np | 4 ms | 635 ms | 316970 ms | 6 ms | 1760ms | SC* | 3 ms | 160 ms | 6466 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+
|Np + RD* | 9 ms | 1281 ms | 419789 ms/BC* | 18 ms | 3052 ms | SC* | 5 ms | 168 ms | 6580 ms |
+-----------+--------+----------+---------------+--------+---------+----------+--------+---------+----------+

*BC = Blender Crash
*SC = Blender Crash + Partial System Crash

Inputs
------

Verts and Edges only. Warning: Does not support faces


Parameters
----------

**Include 'On touch'** : consider a valid intersection when one end of the edge lays on another edge.
Generates double points so if active "remove doubles" switch is recommended. Only for Np and 3d mode

**Doubles**: Merges points that are closer than the defined distance. Only for Alg_1, Np and 3d mode


Parameters N-panel
------------------

Epsilon - For comparing float figures. Does not effect on performance.

Big data Limit - Number of incoming edges where the node will switch to a slower-but-safer implementation of the algorithm (for Alg_1)


Outputs
-------

Vertices and Edges, the mesh does not preserve any old vertex or edge index ordering due to the Hashing algorithm used for fast intersection lookups.


Examples
--------

.. image:: https://cloud.githubusercontent.com/assets/619340/2811581/16032c72-ce26-11e3-9055-925d2cd03719.png

See the progress of how this node came to life `here <https://github.com/nortikin/sverchok/issues/109>`_ (gifs, screenshots)
101 changes: 0 additions & 101 deletions docs/nodes/modifier_change/edges_intersect_mk2.rst

This file was deleted.

1 change: 0 additions & 1 deletion docs/nodes/modifier_change/modifier_change_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Modifier Change
relax_mesh
delete_loose
mesh_clean
edges_intersect_mk2
poke
extrude_edges_mk2
extrude_separate
Expand Down
2 changes: 1 addition & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@

## CAD
SvBevelNode
SvIntersectEdgesNodeMK2
SvIntersectEdgesNodeMK3
SvOffsetNode
SvInsetSpecialMk2
SvInsetFaces
Expand Down
Loading

0 comments on commit a5f8693

Please sign in to comment.