|
| 1 | +import bpy |
| 2 | +from bpy.types import Operator |
| 3 | +from bpy.props import IntProperty |
| 4 | + |
| 5 | +from math import cos, sin, radians, sqrt |
| 6 | +from mathutils import Vector |
| 7 | + |
| 8 | +import logging |
| 9 | +log = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def lonlat2xyz(R, lon, lat): |
| 13 | + lon, lat = radians(lon), radians(lat) |
| 14 | + x = R * cos(lat) * cos(lon) |
| 15 | + y = R * cos(lat) * sin(lon) |
| 16 | + z = R *sin(lat) |
| 17 | + return (x, y, z) |
| 18 | + |
| 19 | + |
| 20 | +class OBJECT_OT_earth_sphere(Operator): |
| 21 | + bl_idname = "earth.sphere" |
| 22 | + bl_label = "lonlat to sphere" |
| 23 | + bl_description = "Transform longitude/latitude data to a sphere like earth globe" |
| 24 | + bl_options = {"REGISTER", "UNDO"} |
| 25 | + |
| 26 | + radius: IntProperty(name = "Radius", default=100, description="Sphere radius", min=1) |
| 27 | + |
| 28 | + def execute(self, context): |
| 29 | + scn = bpy.context.scene |
| 30 | + obj = bpy.context.view_layer.objects.active |
| 31 | + |
| 32 | + if not obj: |
| 33 | + self.report({'INFO'}, "No active object") |
| 34 | + return {'CANCELLED'} |
| 35 | + |
| 36 | + if obj.type != 'MESH': |
| 37 | + self.report({'INFO'}, "Selection isn't a mesh") |
| 38 | + return {'CANCELLED'} |
| 39 | + |
| 40 | + w, h, thick = obj.dimensions |
| 41 | + if w > 360: |
| 42 | + self.report({'INFO'}, "Longitude exceed 360°") |
| 43 | + return {'CANCELLED'} |
| 44 | + if h > 180: |
| 45 | + self.report({'INFO'}, "Latitude exceed 360°") |
| 46 | + return {'CANCELLED'} |
| 47 | + |
| 48 | + mesh = obj.data |
| 49 | + for vertex in mesh.vertices: |
| 50 | + lon, lat = vertex.co.x, vertex.co.y |
| 51 | + vertex.co = lonlat2xyz(self.radius, lon, lat) |
| 52 | + |
| 53 | + return {'FINISHED'} |
| 54 | + |
| 55 | +EARTH_RADIUS = 6378137 #meters |
| 56 | +def getZDelta(d): |
| 57 | + '''delta value for adjusting z across earth curvature |
| 58 | + http://webhelp.infovista.com/Planet/62/Subsystems/Raster/Content/help/analysis/viewshedanalysis.html''' |
| 59 | + return sqrt(EARTH_RADIUS**2 + d**2) - EARTH_RADIUS |
| 60 | + |
| 61 | + |
| 62 | +class OBJECT_OT_earth_curvature(Operator): |
| 63 | + bl_idname = "earth.curvature" |
| 64 | + bl_label = "Earth curvature correction" |
| 65 | + bl_description = "Apply earth curvature correction for viewsheed analysis" |
| 66 | + bl_options = {"REGISTER", "UNDO"} |
| 67 | + |
| 68 | + def execute(self, context): |
| 69 | + scn = bpy.context.scene |
| 70 | + obj = bpy.context.view_layer.objects.active |
| 71 | + |
| 72 | + if not obj: |
| 73 | + self.report({'INFO'}, "No active object") |
| 74 | + return {'CANCELLED'} |
| 75 | + |
| 76 | + if obj.type != 'MESH': |
| 77 | + self.report({'INFO'}, "Selection isn't a mesh") |
| 78 | + return {'CANCELLED'} |
| 79 | + |
| 80 | + mesh = obj.data |
| 81 | + viewpt = scn.cursor.location |
| 82 | + |
| 83 | + for vertex in mesh.vertices: |
| 84 | + d = (viewpt.xy - vertex.co.xy).length |
| 85 | + vertex.co.z = vertex.co.z - getZDelta(d) |
| 86 | + |
| 87 | + return {'FINISHED'} |
| 88 | + |
| 89 | + |
| 90 | +classes = [ |
| 91 | + OBJECT_OT_earth_sphere, |
| 92 | + OBJECT_OT_earth_curvature |
| 93 | +] |
| 94 | + |
| 95 | +def register(): |
| 96 | + for cls in classes: |
| 97 | + try: |
| 98 | + bpy.utils.register_class(cls) |
| 99 | + except ValueError as e: |
| 100 | + log.warning('{} is already registered, now unregister and retry... '.format(cls)) |
| 101 | + bpy.utils.unregister_class(cls) |
| 102 | + bpy.utils.register_class(cls) |
| 103 | + |
| 104 | +def unregister(): |
| 105 | + for cls in classes: |
| 106 | + bpy.utils.unregister_class(cls) |
0 commit comments