Skip to content
Snippets Groups Projects
Surfaces.py 16 KiB
Newer Older
  • Learn to ignore specific revisions
  • 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
    import bpy
    import bmesh
    
    from . import Math
    from . import Curves
    
    
    
    class LoftedSplineSurface:
        def __init__(self, activeSpline, otherSpline, bMesh, vert0Index, resolution):
            self.splineA = activeSpline
            self.splineO = otherSpline
    
            self.bMesh = bMesh
            self.vert0Index = vert0Index
            self.resolution = resolution
    
    
        def Apply(self, worldMatrixA, worldMatrixO):
            #deltaPar = 1.0 / float(self.resolution - 1)
    
            par = 0.0
            pointA = worldMatrixA @ self.splineA.CalcPoint(par)
            pointO = worldMatrixO @ self.splineO.CalcPoint(par)
            self.bMesh.verts[self.vert0Index].co = pointA
            self.bMesh.verts[self.vert0Index + 1].co = pointO
    
            fltResm1 = float(self.resolution - 1)
            for i in range(1, self.resolution):
                par = float(i) / fltResm1
    
                pointA = worldMatrixA @ self.splineA.CalcPoint(par)
                pointO = worldMatrixO @ self.splineO.CalcPoint(par)
                self.bMesh.verts[self.vert0Index + 2 * i].co = pointA
                self.bMesh.verts[self.vert0Index + 2 * i + 1].co = pointO
    
    
        def AddFaces(self):
            currIndexA = self.vert0Index
            currIndexO = self.vert0Index + 1
    
            bmVerts = self.bMesh.verts
            bmVerts.ensure_lookup_table()
    
            for i in range(1, self.resolution):
                nextIndexA = self.vert0Index + 2 * i
                nextIndexO = nextIndexA + 1
    
                self.bMesh.faces.new([bmVerts[currIndexA], bmVerts[currIndexO], bmVerts[nextIndexO], bmVerts[nextIndexA]])
    
                currIndexA = nextIndexA
                currIndexO = nextIndexO
    
    
    class LoftedSurface:
        @staticmethod
        def FromSelection():
            selObjects = bpy.context.selected_objects
            if len(selObjects) != 2: raise Exception("len(selObjects) != 2") # shouldn't be possible
    
            blenderActiveCurve = bpy.context.active_object
            blenderOtherCurve = selObjects[0]
            if blenderActiveCurve == blenderOtherCurve: blenderOtherCurve = selObjects[1]
    
            aCurve = Curves.Curve(blenderActiveCurve)
            oCurve = Curves.Curve(blenderOtherCurve)
    
            name = "TODO: autoname"
    
            return LoftedSurface(aCurve, oCurve, name)
    
    
        def __init__(self, activeCurve, otherCurve, name = "LoftedSurface"):
            self.curveA = activeCurve
            self.curveO = otherCurve
            self.name  = name
    
            self.nrSplines = self.curveA.nrSplines
            if self.curveO.nrSplines < self.nrSplines: self.nrSplines = self.curveO.nrSplines
    
            self.bMesh = bmesh.new()
    
            self.splineSurfaces = self.SetupSplineSurfaces()
    
            self.Apply()
    
    
        def SetupSplineSurfaces(self):
            rvSplineSurfaces = []
    
            currV0Index = 0
            for i in range(self.nrSplines):
                splineA = self.curveA.splines[i]
                splineO = self.curveO.splines[i]
    
                res = splineA.resolution
                if splineO.resolution < res: res = splineO.resolution
    
                for iv in range(2 * res): self.bMesh.verts.new()
    
                splSurf = LoftedSplineSurface(splineA, splineO, self.bMesh, currV0Index, res)
                splSurf.AddFaces()
                rvSplineSurfaces.append(splSurf)
    
                currV0Index += 2 * res
    
            return rvSplineSurfaces
    
    
        def Apply(self):
            for splineSurface in self.splineSurfaces: splineSurface.Apply(self.curveA.worldMatrix, self.curveO.worldMatrix)
    
    
        def AddToScene(self):
            mesh = bpy.data.meshes.new("Mesh" + self.name)
    
            self.bMesh.to_mesh(mesh)
            mesh.update()
    
            meshObject = bpy.data.objects.new(self.name, mesh)
    
            bpy.context.collection.objects.link(meshObject)
    
    
    
    # active spline is swept over other spline (rail)
    class SweptSplineSurface:
        def __init__(self, activeSpline, otherSpline, bMesh, vert0Index, resolutionA, resolutionO):
            self.splineA = activeSpline
            self.splineO = otherSpline
    
            self.bMesh = bMesh
            self.vert0Index = vert0Index
            self.resolutionA = resolutionA
            self.resolutionO = resolutionO
    
    
        def Apply(self, worldMatrixA, worldMatrixO):
            localPointsA = []
            fltResAm1 = float(self.resolutionA - 1)
            for i in range(self.resolutionA):
                par = float(i) / fltResAm1
                pointA = self.splineA.CalcPoint(par)
                localPointsA.append(pointA)
    
    
            worldPointsO = []
            localDerivativesO = []
            fltResOm1 = float(self.resolutionO - 1)
            for i in range(self.resolutionO):
                par = float(i) / fltResOm1
    
                pointO = self.splineO.CalcPoint(par)
                worldPointsO.append(worldMatrixO @ pointO)
    
                derivativeO = self.splineO.CalcDerivative(par)
                localDerivativesO.append(derivativeO)
    
    
            currWorldMatrixA = worldMatrixA
            worldMatrixOInv = worldMatrixO.inverted()
            prevDerivativeO = localDerivativesO[0]
            for iO in range(self.resolutionO):
                currDerivativeO = localDerivativesO[iO]
                localRotMatO = Math.CalcRotationMatrix(prevDerivativeO, currDerivativeO)
    
                currLocalAToLocalO = worldMatrixOInv @ currWorldMatrixA
                worldPointsA = []
                for iA in range(self.resolutionA):
                    pointALocalToO = currLocalAToLocalO @ localPointsA[iA]
                    rotatedPointA = localRotMatO @ pointALocalToO
                    worldPointsA.append(worldMatrixO @ rotatedPointA)
    
                worldOffsetsA = []
                worldPoint0A = worldPointsA[0]
                for i in range(self.resolutionA): worldOffsetsA.append(worldPointsA[i] - worldPoint0A)
    
    
                for iA in range(self.resolutionA):
                    iVert = self.vert0Index + (self.resolutionA * iO) + iA
                    currVert = worldPointsO[iO] + worldOffsetsA[iA]
                    self.bMesh.verts[iVert].co = currVert
    
                prevDerivativeO = currDerivativeO
                currWorldMatrixA = worldMatrixO @ localRotMatO @ currLocalAToLocalO
    
    
        def AddFaces(self):
            bmVerts = self.bMesh.verts
            bmVerts.ensure_lookup_table()
    
            for iO in range(self.resolutionO - 1):
                for iA in range(self.resolutionA - 1):
                    currIndexA1 = self.vert0Index + (self.resolutionA * iO) + iA
                    currIndexA2 = currIndexA1 + 1
                    nextIndexA1 = self.vert0Index + (self.resolutionA * (iO + 1)) + iA
                    nextIndexA2 = nextIndexA1 + 1
    
                    self.bMesh.faces.new([bmVerts[currIndexA1], bmVerts[currIndexA2], bmVerts[nextIndexA2], bmVerts[nextIndexA1]])
    
    
    
    class SweptSurface:
        @staticmethod
        def FromSelection():
            selObjects = bpy.context.selected_objects
            if len(selObjects) != 2: raise Exception("len(selObjects) != 2") # shouldn't be possible
    
            blenderActiveCurve = bpy.context.active_object
            blenderOtherCurve = selObjects[0]
            if blenderActiveCurve == blenderOtherCurve: blenderOtherCurve = selObjects[1]
    
            aCurve = Curves.Curve(blenderActiveCurve)
            oCurve = Curves.Curve(blenderOtherCurve)
    
            name = "TODO: autoname"
    
            return SweptSurface(aCurve, oCurve, name)
    
    
        def __init__(self, activeCurve, otherCurve, name = "SweptSurface"):
            self.curveA = activeCurve
            self.curveO = otherCurve
            self.name  = name
    
            self.nrSplines = self.curveA.nrSplines
            if self.curveO.nrSplines < self.nrSplines: self.nrSplines = self.curveO.nrSplines
    
            self.bMesh = bmesh.new()
    
            self.splineSurfaces = self.SetupSplineSurfaces()
    
            self.Apply()
    
    
        def SetupSplineSurfaces(self):
            rvSplineSurfaces = []
    
            currV0Index = 0
            for i in range(self.nrSplines):
                splineA = self.curveA.splines[i]
                splineO = self.curveO.splines[i]
    
                resA = splineA.resolution
                resO = splineO.resolution
    
                for iv in range(resA * resO): self.bMesh.verts.new()
    
                splSurf = SweptSplineSurface(splineA, splineO, self.bMesh, currV0Index, resA, resO)
                splSurf.AddFaces()
                rvSplineSurfaces.append(splSurf)
    
                currV0Index += resA * resO
    
            return rvSplineSurfaces
    
    
        def Apply(self):
            for splineSurface in self.splineSurfaces: splineSurface.Apply(self.curveA.worldMatrix, self.curveO.worldMatrix)
    
    
        def AddToScene(self):
            mesh = bpy.data.meshes.new("Mesh" + self.name)
    
            self.bMesh.to_mesh(mesh)
            mesh.update()
    
            meshObject = bpy.data.objects.new(self.name, mesh)
    
            bpy.context.collection.objects.link(meshObject)
    
    
    
    # profileSpline is swept over rail1Spline and scaled/rotated to have its endpoint on rail2Spline
    class BirailedSplineSurface:
        def __init__(self, rail1Spline, rail2Spline, profileSpline, bMesh, vert0Index, resolutionRails, resolutionProfile):
            self.rail1Spline = rail1Spline
            self.rail2Spline = rail2Spline
            self.profileSpline = profileSpline
    
            self.bMesh = bMesh
            self.vert0Index = vert0Index
            self.resolutionRails = resolutionRails
            self.resolutionProfile = resolutionProfile
    
    
        def Apply(self, worldMatrixRail1, worldMatrixRail2, worldMatrixProfile):
            localPointsProfile = []
            fltResProfilem1 = float(self.resolutionProfile - 1)
            for i in range(self.resolutionProfile):
                par = float(i) / fltResProfilem1
                pointProfile = self.profileSpline.CalcPoint(par)
                localPointsProfile.append(pointProfile)
    
    
            worldPointsRail1 = []
            localDerivativesRail1 = []
            worldPointsRail2 = []
            fltResRailsm1 = float(self.resolutionRails - 1)
            for i in range(self.resolutionRails):
                par = float(i) / fltResRailsm1
    
                pointRail1 = self.rail1Spline.CalcPoint(par)
                worldPointsRail1.append(worldMatrixRail1 @ pointRail1)
    
                derivativeRail1 = self.rail1Spline.CalcDerivative(par)
                localDerivativesRail1.append(derivativeRail1)
    
                pointRail2 = self.rail2Spline.CalcPoint(par)
                worldPointsRail2.append(worldMatrixRail2 @ pointRail2)
    
    
            currWorldMatrixProfile = worldMatrixProfile
            worldMatrixRail1Inv = worldMatrixRail1.inverted()
            prevDerivativeRail1 = localDerivativesRail1[0]
            for iRail in range(self.resolutionRails):
                currDerivativeRail1 = localDerivativesRail1[iRail]
                localRotMatRail1 = Math.CalcRotationMatrix(prevDerivativeRail1, currDerivativeRail1)
    
                currLocalProfileToLocalRail1 = worldMatrixRail1Inv @ currWorldMatrixProfile
                worldPointsProfileRail1 = []
                for iProfile in range(self.resolutionProfile):
                    pointProfileLocalToRail1 = currLocalProfileToLocalRail1 @ localPointsProfile[iProfile]
                    rotatedPointProfile = localRotMatRail1 @ pointProfileLocalToRail1
                    worldPointsProfileRail1.append(worldMatrixRail1 @ rotatedPointProfile)
    
                worldOffsetsProfileRail1 = []
                worldPoint0ProfileRail1 = worldPointsProfileRail1[0]
                for iProfile in range(self.resolutionProfile): worldOffsetsProfileRail1.append(worldPointsProfileRail1[iProfile] - worldPoint0ProfileRail1)
    
                worldStartPointProfileRail1 = worldPointsRail1[iRail]
                worldEndPointProfileRail1 = worldStartPointProfileRail1 + worldOffsetsProfileRail1[-1]
                v3From = worldEndPointProfileRail1 - worldStartPointProfileRail1
                v3To = worldPointsRail2[iRail] - worldStartPointProfileRail1
                if not v3From.magnitude == 0:
                    scaleFactorRail2 = v3To.magnitude / v3From.magnitude
                else:
                    scaleFactorRail2 = 1
                rotMatRail2 = Math.CalcRotationMatrix(v3From, v3To)
    
                worldOffsetsProfileRail2 = []
                for iProfile in range(self.resolutionProfile):
                    offsetProfileRail1 = worldOffsetsProfileRail1[iProfile]
                    worldOffsetsProfileRail2.append(rotMatRail2 @ (offsetProfileRail1 * scaleFactorRail2))
    
    
                for iProfile in range(self.resolutionProfile):
                    iVert = self.vert0Index + (self.resolutionProfile * iRail) + iProfile
                    currVert = worldPointsRail1[iRail] + worldOffsetsProfileRail2[iProfile]
                    self.bMesh.verts[iVert].co = currVert
    
                prevDerivativeRail1 = currDerivativeRail1
                currWorldMatrixProfile = worldMatrixRail1 @ localRotMatRail1 @ currLocalProfileToLocalRail1
    
    
        def AddFaces(self):
            bmVerts = self.bMesh.verts
            bmVerts.ensure_lookup_table()
    
            for iRail in range(self.resolutionRails - 1):
                for iProfile in range(self.resolutionProfile - 1):
                    currIndex1 = self.vert0Index + (self.resolutionProfile * iRail) + iProfile
                    currIndex2 = currIndex1 + 1
                    nextIndex1 = self.vert0Index + (self.resolutionProfile * (iRail + 1)) + iProfile
                    nextIndex2 = nextIndex1 + 1
    
                    self.bMesh.faces.new([bmVerts[currIndex1], bmVerts[currIndex2], bmVerts[nextIndex2], bmVerts[nextIndex1]])
    
    
    
    class BirailedSurface:
        @staticmethod
        def FromSelection():
            nrSelectedObjects = bpy.context.scene.curvetools.NrSelectedObjects
            if nrSelectedObjects != 3: raise Exception("nrSelectedObjects != 3") # shouldn't be possible
    
    
            selectedObjects = bpy.context.scene.curvetools.SelectedObjects
            selectedObjectValues = selectedObjects.values()
    
            curveName = selectedObjectValues[0].name
            rail1BlenderCurve = None
            try: rail1BlenderCurve = bpy.data.objects[curveName]
            except: rail1BlenderCurve = None
            if rail1BlenderCurve is None: raise Exception("rail1BlenderCurve is None")
    
            curveName = selectedObjectValues[1].name
            rail2BlenderCurve = None
            try: rail2BlenderCurve = bpy.data.objects[curveName]
            except: rail2BlenderCurve = None
            if rail2BlenderCurve is None: raise Exception("rail2BlenderCurve is None")
    
            curveName = selectedObjectValues[2].name
            profileBlenderCurve = None
            try: profileBlenderCurve = bpy.data.objects[curveName]
            except: profileBlenderCurve = None
            if profileBlenderCurve is None: raise Exception("profileBlenderCurve is None")
    
    
            rail1Curve = Curves.Curve(rail1BlenderCurve)
            rail2Curve = Curves.Curve(rail2BlenderCurve)
            profileCurve = Curves.Curve(profileBlenderCurve)
    
            name = "TODO: autoname"
    
            return BirailedSurface(rail1Curve, rail2Curve, profileCurve, name)
    
    
        def __init__(self, rail1Curve, rail2Curve, profileCurve, name = "BirailedSurface"):
            self.rail1Curve = rail1Curve
            self.rail2Curve = rail2Curve
            self.profileCurve = profileCurve
            self.name  = name
    
            self.nrSplines = self.rail1Curve.nrSplines
            if self.rail2Curve.nrSplines < self.nrSplines: self.nrSplines = self.rail2Curve.nrSplines
            if self.profileCurve.nrSplines < self.nrSplines: self.nrSplines = self.profileCurve.nrSplines
    
            self.bMesh = bmesh.new()
    
            self.splineSurfaces = self.SetupSplineSurfaces()
    
            self.Apply()
    
    
        def SetupSplineSurfaces(self):
            rvSplineSurfaces = []
    
            currV0Index = 0
            for i in range(self.nrSplines):
                splineRail1 = self.rail1Curve.splines[i]
                splineRail2 = self.rail2Curve.splines[i]
                splineProfile = self.profileCurve.splines[i]
    
                resProfile = splineProfile.resolution
                resRails = splineRail1.resolution
                if splineRail2.resolution < resRails: resRails = splineRail2.resolution
    
                for iv in range(resProfile * resRails): self.bMesh.verts.new()
    
                splSurf = BirailedSplineSurface(splineRail1, splineRail2, splineProfile, self.bMesh, currV0Index, resRails, resProfile)
                splSurf.AddFaces()
                rvSplineSurfaces.append(splSurf)
    
                currV0Index += resProfile * resRails
    
            return rvSplineSurfaces
    
    
        def Apply(self):
            for splineSurface in self.splineSurfaces: splineSurface.Apply(self.rail1Curve.worldMatrix, self.rail2Curve.worldMatrix, self.profileCurve.worldMatrix)
    
    
        def AddToScene(self):
            mesh = bpy.data.meshes.new("Mesh" + self.name)
    
            self.bMesh.to_mesh(mesh)
            mesh.update()
    
            meshObject = bpy.data.objects.new(self.name, mesh)
    
            bpy.context.collection.objects.link(meshObject)