第二章:Pythonocc官方demo 案例92(自定义graphic3d类来渲染显示大量线段)

源代码:

from __future__ import print_function

import random
import warnings

from OCC.Core.Aspect import Aspect_TOL_SOLID
from OCC.Display.SimpleGui import init_display
from OCC.Core.Graphic3d import Graphic3d_ArrayOfPolylines, Graphic3d_AspectLine3d
from OCC.Core.Prs3d import Prs3d_Root_CurrentGroup
from OCC.Core.Quantity import Quantity_NOC_BLACK, Quantity_Color
from OCC.Core.gp import gp_Pnt
from OCC.Core.Graphic3d import Graphic3d_Structure


def create_ogl_group(display):
    """
    create a group that will store an OpenGL buffer
    """
    aStructure = Graphic3d_Structure(display._struc_mgr)
    group = Prs3d_Root_CurrentGroup(aStructure)
    return aStructure, group


def generate_points(spread, n):
    try:
        import numpy as np
        arr = np.random.uniform(-spread / 2.0, spread / 2.0, (n, 3))
        for i in arr:
            yield i
    except ImportError:
        n_ = n / 100
        warnings.warn("Numpy could not be imported... this example will run very SLOW"
                      "drawing {} rather than {} lines".format(n_, n))
        for i in range(n_):
            a = random.uniform(-spread / 2.0, spread / 2.0)
            b = random.uniform(-spread / 2.0, spread / 2.0)
            c = random.uniform(-spread / 2.0, spread / 2.0)
            yield (a, b, c)


def draw_lines(pnt_list, nr_of_points, display):
    """

    rendering a large number of points through the usual way of:

        display.DisplayShape( make_vertex( gp_Pnt() ) )

    is fine for TopoDS_Shapes but certainly not for large number of points.
    in comparison, drawing all the voxel samples takes 18sec using the approach above, but negigable when using this function
    its about 2 orders of Magnitude faster, so worth the extra hassle

    here we use a more close-to-the-metal approach of drawing directly in OpenGL

    see [1] for a more detailed / elegant way to perform this task

    [1] http://www.opencascade.org/org/forum/thread_21732/?forum=3

    Parameters
    ----------

    pnt_list: list of (x,y,z) tuples
        vertex list

    display: qtViewer3d

    """

    a_presentation, group = create_ogl_group(display)
    black = Quantity_Color(Quantity_NOC_BLACK)
    asp = Graphic3d_AspectLine3d(black, Aspect_TOL_SOLID, 1)

    gg = Graphic3d_ArrayOfPolylines(nr_of_points * 2,
                                    nr_of_points * 2,
                                    0,  # maxEdges
                                    False,  # hasVColors
                                    True
                                    )

    try:
        while 1:
            pnt = gp_Pnt(*next(pnt_list))
            gg.AddVertex(pnt)
            pnt = gp_Pnt(*next(pnt_list))
            gg.AddVertex(pnt)
            # create the line, with a random color
            gg.AddBound(2, random.random(), random.random(), random.random())

    except StopIteration:
        pass

    group.SetPrimitivesAspect(asp)
    group.AddPrimitiveArray(gg)
    a_presentation.Display()


display, start_display, add_menu, add_function_to_menu = init_display()
nr_of_points = 100000
spread = 100
draw_lines(generate_points(spread, nr_of_points), nr_of_points, display)
start_display()

运行效果:自定义graphic3d类使用类内的group来储存opengl缓存,提高显示效率,减少渲染时间。
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

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

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

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

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

打赏作者

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

抵扣说明:

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

余额充值