Skip to content

Commit

Permalink
reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
KatKatKateryna committed Jan 15, 2024
1 parent 067e0f2 commit 4530aab
Show file tree
Hide file tree
Showing 15 changed files with 385 additions and 534 deletions.
4 changes: 0 additions & 4 deletions speckle/converter/geometry/GisGeometryClasses.py

This file was deleted.

161 changes: 91 additions & 70 deletions speckle/converter/geometry/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
import math
import numpy as np

from qgis.core import (
QgsPoint,
QgsPointXY, QgsFeature, QgsVectorLayer, QgsUnitTypes
)
from qgis.core import QgsPoint, QgsPointXY, QgsFeature, QgsVectorLayer

from specklepy.objects.geometry import Point
from speckle.converter.layers.utils import get_scale_factor, get_scale_factor_to_meter
from speckle.converter.layers.utils import get_scale_factor
from speckle.converter.layers.symbology import featureColorfromNativeRenderer
from speckle.utils.panel_logging import logToUser
#from PyQt5.QtGui import QColor

def applyOffsetsRotation(x, y, dataStorage): # on Send

def applyOffsetsRotation(x: float, y: float, dataStorage): # on Send
try:
offset_x = dataStorage.crs_offset_x
offset_y = dataStorage.crs_offset_y
Expand All @@ -22,28 +19,27 @@ def applyOffsetsRotation(x, y, dataStorage): # on Send
x -= offset_x
if offset_y is not None and isinstance(offset_y, float):
y -= offset_y
if rotation is not None and isinstance(rotation, float) and -360< rotation <360:
if (
rotation is not None
and isinstance(rotation, float)
and -360 < rotation < 360
):
a = rotation * math.pi / 180
x2 = x
y2 = y

#if a < 0: # turn counterclockwise on send
# x2 = x*math.cos(a) - y*math.sin(a)
# y2 = x*math.sin(a) + y*math.cos(a)

#elif a > 0: # turn clockwise on send
x2 = x*math.cos(a) + y*math.sin(a)
y2 = -x*math.sin(a) + y*math.cos(a)
x2 = x * math.cos(a) + y * math.sin(a)
y2 = -x * math.sin(a) + y * math.cos(a)
x = x2
y = y2
return x, y
except Exception as e:
logToUser(e, level = 2, func = inspect.stack()[0][3])
return None, None
logToUser(e, level=2, func=inspect.stack()[0][3])
return None, None


def pointToSpeckle(pt: QgsPoint or QgsPointXY, feature: QgsFeature, layer: QgsVectorLayer, dataStorage):
def pointToSpeckle(
pt: QgsPoint or QgsPointXY, feature: QgsFeature, layer: QgsVectorLayer, dataStorage
):
"""Converts a QgsPoint to Speckle"""
try:
try:
if isinstance(pt, QgsPointXY):
pt = QgsPoint(pt)
# when unset, z() returns "nan"
Expand All @@ -59,94 +55,116 @@ def pointToSpeckle(pt: QgsPoint or QgsPointXY, feature: QgsFeature, layer: QgsVe
specklePoint.x, specklePoint.y = applyOffsetsRotation(x, y, dataStorage)

col = featureColorfromNativeRenderer(feature, layer)
specklePoint['displayStyle'] = {}
specklePoint['displayStyle']['color'] = col
specklePoint["displayStyle"] = {}
specklePoint["displayStyle"]["color"] = col
return specklePoint
except Exception as e:
logToUser(e, level = 2, func = inspect.stack()[0][3])
logToUser(e, level=2, func=inspect.stack()[0][3])
return None

def transformSpecklePt(pt_original: Point, dataStorage) -> Point: # on Receive

def transformSpecklePt(pt_original: Point, dataStorage) -> Point: # on Receive
offset_x = dataStorage.crs_offset_x
offset_y = dataStorage.crs_offset_y
rotation = dataStorage.crs_rotation

pt = Point(x=pt_original.x, y=pt_original.y, z=pt_original.z, units = pt_original.units)
pt = Point(
x=pt_original.x, y=pt_original.y, z=pt_original.z, units=pt_original.units
)

gisLayer = None
try:
gisLayer = dataStorage.latestHostApp.lower().endswith("gis")
#print(gisLayer)
applyTransforms = False if (gisLayer and gisLayer is True) else True
except Exception as e:
print(e)
applyTransforms = True
# print(gisLayer)
applyTransforms = False if (gisLayer and gisLayer is True) else True
except Exception as e:
print(e)
applyTransforms = True

# for non-GIS layers
# for non-GIS layers
if applyTransforms is True:
#print("transform non-gis layer")
if rotation is not None and isinstance(rotation, float) and -360< rotation <360:
# print("transform non-gis layer")
if (
rotation is not None
and isinstance(rotation, float)
and -360 < rotation < 360
):
a = rotation * math.pi / 180
x2 = pt.x
y2 = pt.y

#if a > 0: # turn counterclockwise on receive
x2 = pt.x*math.cos(a) - pt.y*math.sin(a)
y2 = pt.x*math.sin(a) + pt.y*math.cos(a)
#elif a < 0: # turn clockwise on receive
# if a > 0: # turn counterclockwise on receive
x2 = pt.x * math.cos(a) - pt.y * math.sin(a)
y2 = pt.x * math.sin(a) + pt.y * math.cos(a)
# elif a < 0: # turn clockwise on receive
# x2 = pt.x*math.cos(a) + pt.y*math.sin(a)
# y2 = -1*pt.x*math.sin(a) + pt.y*math.cos(a)
# y2 = -1*pt.x*math.sin(a) + pt.y*math.cos(a)

pt.x = x2
pt.y = y2
if offset_x is not None and isinstance(offset_x, float) and offset_y is not None and isinstance(offset_y, float):
if (
offset_x is not None
and isinstance(offset_x, float)
and offset_y is not None
and isinstance(offset_y, float)
):
pt.x += offset_x
pt.y += offset_y

# for GIS layers
if gisLayer is True:
#print("transform GIS layer")
if gisLayer is True:
# print("transform GIS layer")
try:
offset_x = dataStorage.current_layer_crs_offset_x
offset_y = dataStorage.current_layer_crs_offset_y
rotation = dataStorage.current_layer_crs_rotation

if rotation is not None and isinstance(rotation, float) and -360< rotation <360:

if (
rotation is not None
and isinstance(rotation, float)
and -360 < rotation < 360
):
a = rotation * math.pi / 180
x2 = pt.x
y2 = pt.y

#if a > 0: # turn counterclockwise on receive
x2 = pt.x*math.cos(a) - pt.y*math.sin(a)
y2 = pt.x*math.sin(a) + pt.y*math.cos(a)
#elif a < 0: # turn clockwise on receive
# if a > 0: # turn counterclockwise on receive
x2 = pt.x * math.cos(a) - pt.y * math.sin(a)
y2 = pt.x * math.sin(a) + pt.y * math.cos(a)
# elif a < 0: # turn clockwise on receive
# x2 = pt.x*math.cos(a) + pt.y*math.sin(a)
# y2 = -1*pt.x*math.sin(a) + pt.y*math.cos(a)
# y2 = -1*pt.x*math.sin(a) + pt.y*math.cos(a)

pt.x = x2
pt.y = y2
if offset_x is not None and isinstance(offset_x, float) and offset_y is not None and isinstance(offset_y, float):
if (
offset_x is not None
and isinstance(offset_x, float)
and offset_y is not None
and isinstance(offset_y, float)
):
pt.x += offset_x
pt.y += offset_y
except Exception as e:
print(e)
except Exception as e:
print(e)

#print(pt)
# print(pt)
return pt


def pointToNativeWithoutTransforms(pt: Point, dataStorage) -> QgsPoint:
"""Converts a Speckle Point to QgsPoint"""
try:
pt = scalePointToNative(pt, pt.units, dataStorage)
pt = applyTransformMatrix(pt, dataStorage)
newPt = pt #transformSpecklePt(pt, dataStorage)
newPt = pt # transformSpecklePt(pt, dataStorage)

return QgsPoint(newPt.x, newPt.y, newPt.z)
except Exception as e:
logToUser(e, level = 2, func = inspect.stack()[0][3])
logToUser(e, level=2, func=inspect.stack()[0][3])
return None


def pointToNative(pt: Point, dataStorage) -> QgsPoint:
"""Converts a Speckle Point to QgsPoint"""
try:
Expand All @@ -156,32 +174,35 @@ def pointToNative(pt: Point, dataStorage) -> QgsPoint:

return QgsPoint(newPt.x, newPt.y, newPt.z)
except Exception as e:
logToUser(e, level = 2, func = inspect.stack()[0][3])
logToUser(e, level=2, func=inspect.stack()[0][3])
return None


def applyTransformMatrix(pt: Point, dataStorage):
try:
if dataStorage.matrix is not None:
#print(f"__PT: {(pt.x, pt.y, pt.z)}")
#print(dataStorage.matrix)
b = np.matrix([pt.x, pt.y, pt.z, 1])
res = b* dataStorage.matrix
#print(res)
x,y,z = res.item(0), res.item(1), res.item(2)
#print(f"__PT: {(x, y, z)}")
return Point(x=x, y=y, z=z, units = pt.units)
except Exception as e: print(e)
return pt
# print(f"__PT: {(pt.x, pt.y, pt.z)}")
# print(dataStorage.matrix)
b = np.matrix([pt.x, pt.y, pt.z, 1])
res = b * dataStorage.matrix
# print(res)
x, y, z = res.item(0), res.item(1), res.item(2)
# print(f"__PT: {(x, y, z)}")
return Point(x=x, y=y, z=z, units=pt.units)
except Exception as e:
print(e)
return pt


def scalePointToNative(point: Point, units: str, dataStorage) -> Point:
"""Scale point coordinates to meters"""
try:
scaleFactor = get_scale_factor(units, dataStorage) # to meters
pt = Point(units = "m")
scaleFactor = get_scale_factor(units, dataStorage) # to meters
pt = Point(units="m")
pt.x = point.x * scaleFactor
pt.y = point.y * scaleFactor
pt.z = 0 if math.isnan(point.z) else point.z * scaleFactor
return pt
except Exception as e:
logToUser(e, level = 2, func = inspect.stack()[0][3])
logToUser(e, level=2, func=inspect.stack()[0][3])
return None
74 changes: 74 additions & 0 deletions speckle/converter/geometry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from specklepy.objects import Base
from typing import List, Tuple, Union, Dict
from speckle.converter.geometry.point import applyOffsetsRotation
from specklepy.objects.geometry import Mesh

from shapely.geometry import Polygon
from shapely.ops import triangulate
Expand Down Expand Up @@ -228,6 +229,79 @@ def to_triangles(data, attempt=0):
return None, None


def trianglateQuadMesh(mesh: Mesh) -> Mesh:
new_mesh = None
try:
new_v: List[float] = []
new_f: List[int] = []
new_c: List[int] = []

# fill new color and vertices lists
used_ind = []
for i, c in enumerate(mesh.colors):
try:
# new_c.append(c)
# continue
if i not in used_ind:
new_c.extend(
[
mesh.colors[i],
mesh.colors[i + 1],
mesh.colors[i + 2],
mesh.colors[i + 2],
mesh.colors[i + 3],
mesh.colors[i],
]
)
used_ind.extend([i, i + 1, i + 2, i + 3])
except Exception as e:
print(e)

used_ind = []
for i, v in enumerate(mesh.vertices):
try:
if i not in used_ind:
v0 = [mesh.vertices[i], mesh.vertices[i + 1], mesh.vertices[i + 2]]
v1 = [
mesh.vertices[i + 3],
mesh.vertices[i + 4],
mesh.vertices[i + 5],
]
v2 = [
mesh.vertices[i + 6],
mesh.vertices[i + 7],
mesh.vertices[i + 8],
]
v3 = [
mesh.vertices[i + 9],
mesh.vertices[i + 10],
mesh.vertices[i + 11],
]

new_v.extend(v0 + v1 + v2 + v2 + v3 + v0)
new_f.extend(
[
int(3),
int(i / 12),
int(i / 12) + 1,
int(i / 12) + 2,
int(3),
int(i / 12) + 3,
int(i / 12) + 4,
int(i / 12) + 5,
]
)
used_ind.extend(list(range(i, i + 12)))
except Exception as e:
print(e)
new_mesh = Mesh.create(new_v, new_f, new_c)
new_mesh.units = mesh.units
except Exception as e:
print(e)
pass
return new_mesh


def getPolyPtsSegments(geom, dataStorage):
vertices = []
vertices3d = []
Expand Down
Loading

0 comments on commit 4530aab

Please sign in to comment.