第二章:Pythonocc官方demo 案例79(基于拓扑结构的高度图)

源代码:

##Copyright 2017 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import division, print_function

import math

from OCC.Core.gp import gp_Pnt
from OCC.Core.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge,
                                     BRepBuilderAPI_MakeFace,
                                     BRepBuilderAPI_MakeWire)
from OCC.Core.TColgp import TColgp_Array2OfPnt
from OCC.Core.GeomAPI import GeomAPI_PointsToBSplineSurface
from OCC.Core.GeomFill import GeomFill_SimpleBound, GeomFill_ConstrainedFilling
from OCC.Core.GeomAbs import GeomAbs_C2
from OCC.Display.SimpleGui import init_display
from OCC.Core.BRepAdaptor import BRepAdaptor_CompCurve, BRepAdaptor_HCompCurve

try:
    from PIL import Image
    HAVE_PIL = True
except ImportError:
    print("PIL not found. Heightmap from image not available.")
    HAVE_PIL = False


def x2_y2(event=None):
    def f(x, y):
        """ Returns z = f(x,y)
        """
        z = x*x - y*y
        return z
    heightmap_from_equation(f)

def cosxsinxcosysiny(event=None):
    def f(x, y):
        z = 5 * math.cos(x) * math.sin(x) * math.sin(y) * math.cos(y)
        return z
    heightmap_from_equation(f, 0, math.pi, 0, math.pi)

def heightmap_from_equation(f, x_min=-1, x_max=1, y_min=-1, y_max=1):
    """ takes an equation z= f(x,y)
    and plot the related point cloud as a bspline surface
    """
    print("compute surface")
    n = 100
    # initialize x axis
    step_x = (x_max - x_min) / n
    x_ = []
    for i in range(n):
        x_.append(x_min + i* step_x)
    # initialize y axis
    step_y = (y_max - y_min) / n
    y_ = []
    for i in range(n):
        y_.append(y_min + i* step_y)
    # compute z
    array = TColgp_Array2OfPnt(1, len(x_), 1, len(y_))
    i = 1
    for x in x_:
        j = 1
        for y in y_:
            z = f(x, y)
            point_to_add = gp_Pnt(x, y, z)
            array.SetValue(i, j, point_to_add)
            j += 1
        i += 1
    print("bspline surface creation")
    bspl_surface = GeomAPI_PointsToBSplineSurface(array, 3, 8, GeomAbs_C2, 0.001).Surface()
    display.DisplayShape(bspl_surface, update=True)

def boundary_curve_from_2_points(p1, p2):
    # first create an edge
    e0 = BRepBuilderAPI_MakeEdge(p1, p2).Edge()
    w0 = BRepBuilderAPI_MakeWire(e0).Wire()
    # boundary for filling
    adap = BRepAdaptor_CompCurve(w0)
    p0_h = BRepAdaptor_HCompCurve(adap)
    boundary = GeomFill_SimpleBound(p0_h, 1e-6, 1e-6)
    return boundary

def heightmap_from_image(event=None):
    """ takes the heightmap from a jpeg file
    and apply a texture
    this example requires numpy/matplotlib
    """
    print("opening image")
    heightmap = Image.open('../assets/images/mountain_heightmap.jpg')
    heightmap.show()
    width = heightmap.size[0]
    height = heightmap.size[1]
    # create the gp_Pnt array
    print("parse image and fill in point array")
    for i in range(1, width):
        for j in range(1, height):
            # all 3 RGB values are equal, just take the first one
            # vertex 1
            height_value = heightmap.getpixel((i-1, j-1))[0]
            v1 = gp_Pnt(i, j, float(height_value)/10)
            # vertex 2
            height_value = heightmap.getpixel((i, j-1))[0]
            v2 = gp_Pnt(i+1, j, float(height_value)/10)
            # vertex 3
            height_value = heightmap.getpixel((i, j))[0]
            v3 = gp_Pnt(i+1, j+1, float(height_value)/10)
            # vertex 4
            height_value = heightmap.getpixel((i-1, j))[0]
            v4 = gp_Pnt(i, j+1, float(height_value)/10)
            # boundaries
            b1 = boundary_curve_from_2_points(v1, v2)
            b2 = boundary_curve_from_2_points(v2, v3)
            b3 = boundary_curve_from_2_points(v3, v4)
            b4 = boundary_curve_from_2_points(v4, v1)
            #
            bConstrainedFilling = GeomFill_ConstrainedFilling(8, 2)
            bConstrainedFilling.Init(b1, b2, b3, b4, False)
            srf1 = bConstrainedFilling.Surface()
            # make a face from this srf
            patch = BRepBuilderAPI_MakeFace()
            bounds = True
            toldegen = 1e-6
            patch.Init(srf1, bounds, toldegen)
            patch.Build()
            display.DisplayShape(patch.Face())
            # then create faces
        print("%s%%" % int(float(i) / width * 100))
        #display.process_events()
    display.FitAll()
    # finally display image
    heightmap.show()

if __name__ == "__main__":
    display, start_display, add_menu, add_function_to_menu = init_display()
    add_menu('heightmap')
    add_function_to_menu('heightmap', x2_y2)
    add_function_to_menu('heightmap', cosxsinxcosysiny)
    if HAVE_PIL:
        add_function_to_menu('heightmap', heightmap_from_image)
    start_display()

运行效果:生成基于拓扑结构的高度图
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁波莱布尼茨信息技术有限公司

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值