Split Shape by Plane in OpenCASCADE
Split Shape by Plane in OpenCASCADE
Abstract. Sometimes you want to split a shape by plane or even split a shape by a B Spline surface, OpenCASCADE provide a feature class BRepFeat_SplitShape to implement the function. The paper give a sample code to split a cylinder by plane.
Key Words. Split Shape, BRep Feature Algorithms.
1. Introduction
OpenCASCADE提供了Boolean Operation实现了任意两个形状的交、并、差的布尔操作。但是如何实现用一个面将一个形状切割成两半呢?其实Boolean Operation中已经有求交分割的算法,但是没有直接提供一个分割的功能类,而是在BRepFeat_SplitShape提供了分割功能。
Figure 1. Split Cylinder by Plane
2. Code Demo
使用类BRepAlgoAPI_Section和类BRepFeat_SplitShape相结合来实现分割Split形状的功能。完整代码示例如下:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->/*
Copyright(C) 2017 Shing Liu(eryar@163.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// Visual Studio 2013 & OpenCASCADE7.1.0
#include <gp_Pln.hxx>
#include <TopoDS.hxx>
#include <TopExp.hxx>
#include <TopExp_Explorer.hxx>
#include <BRepTools.hxx>
#include <BRepPrimAPI_MakeCylinder.hxx>
#include <BRepAlgoAPI_Section.hxx>
#include <BRepFeat_SplitShape.hxx>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG2d.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TKGeomAlgo.lib")
#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKTopAlgo.lib")
#pragma comment(lib, "TKBO.lib")
#pragma comment(lib, "TKPrim.lib")
#pragma comment(lib, "TKFeat.lib")
//! Test split a cylinder by plane.
//! You can use the algorithm to split other shapes.
void testSplit()
{
BRepPrimAPI_MakeCylinder aCylinderMaker(10.0, 20.0);
TopoDS_Shape aCylinder = aCylinderMaker.Shape();
// Build section by the split plane for the cylinder.
BRepAlgoAPI_Section aSection(aCylinder, gp_Pln(gp_Pnt(0.0, 0.0, 15.0), gp::DZ()), Standard_False);
aSection.ComputePCurveOn1(Standard_True);
aSection.Approximation(Standard_True);
aSection.Build();
// Split the cylinder shape.
BRepFeat_SplitShape aShapeSpliter(aCylinder);
for (TopExp_Explorer i(aSection.Shape(), TopAbs_EDGE); i.More(); i.Next())
{
TopoDS_Shape anEdge = i.Current();
TopoDS_Shape aFace;
if (aSection.HasAncestorFaceOn1(anEdge, aFace))
{
TopoDS_Edge E = TopoDS::Edge(anEdge);
TopoDS_Face F = TopoDS::Face(aFace);
aShapeSpliter.Add(E, F);
}
}
aShapeSpliter.Build();
// Rebuild left and right shape.
BRep_Builder aBuilder;
TopoDS_Compound aLeftCompound;
TopoDS_Compound aRightCompound;
aBuilder.MakeCompound(aLeftCompound);
aBuilder.MakeCompound(aRightCompound);
// Left shape.
TopTools_MapOfShape aLeftShapeMap;
const TopTools_ListOfShape& aLeftShapes = aShapeSpliter.Left();
for (auto i = aLeftShapes.cbegin(); i != aLeftShapes.cend(); i++)
{
aLeftShapeMap.Add(*i);
aBuilder.Add(aLeftCompound, *i);
}
// Right shape.
TopTools_IndexedMapOfShape aShapeMap;
TopExp::MapShapes(aShapeSpliter.Shape(), TopAbs_FACE, aShapeMap);
for (auto i = aShapeMap.cbegin(); i != aShapeMap.cend(); i++)
{
if (!aLeftShapeMap.Contains(*i))
{
aBuilder.Add(aRightCompound, *i);
}
}
// Output left and right shape.
BRepTools::Write(aLeftCompound, "d:/left.brep");
BRepTools::Write(aRightCompound, "d:/right.brep");
}
int main(int argc, char* argv[])
{
testSplit();
return 0;
}
先创建一个圆柱体,再使用类BRepAlgoAPI_Section将圆柱体用平面进行分割,最后使用类BRepFeat_SplitShape进行分类,得到切割后的形状及Left()形状,把切割后形状中的Left过滤后剩下就是切割另一半的形状;最后导出被平面切割后得到的两半形状。结果用动画演示如下:
Figure 2. Split Shape animation demo
3. Conclusion
OpenCASCADE提供类BRepFeat_SplitShape来实现对一个形状进行切割的功能,但是要配合BRepAlgoAPI_Section使用。因为Boolean Operation中已经实现了求交、分类的功能,所以在最新版本的源码7.2.0中已经将分割功能集成到了Boolean Operation中。分割后如果没有被改变的面还是原来的面。
Split Shape by Plane in OpenCASCADE的更多相关文章
- OpenCASCADE PCurve of Topological Face
OpenCASCADE PCurve of Topological Face eryar@163.com Abstract. OpenCASCADE provides a class BRepBuil ...
- Mesh Algorithm in OpenCascade
Mesh Algorithm in OpenCascade eryar@163.com Abstract. Rendering a generic surface is a two steps pro ...
- FreeType in OpenCASCADE
FreeType in OpenCASCADE eryar@163.com Abstract. FreeType is required for text display in the 3D view ...
- Egret中使用P2物理引擎
游戏中的对象按照物理规律移动,体现重力.引力.反作用力.加速度等物体特性,实现自由落体.摇摆运动.抛物线运动,以及物理碰撞现象的模拟.用于模拟物理碰撞.物理运动的引擎称为物理引擎. 来自瑞典斯德哥尔摩 ...
- 那些我离不开的 Sketch 插件
当谈论到插件时,我是一名极客.各种新颖的 Sketch 插件层出不穷,但是有那么几个是我怎么也离不开的. Sketch 运行器 多层的插件菜单再也不会影响我的效率了. 我推迟了好几年才使用这个插件,因 ...
- p2 形状
形状是物理引擎进行碰撞模拟计算的依据,是刚体最基本的属性. P2中使用Shape类来表示形状,通过刚体的addShape()方法,将形状添加到刚体中之后, 就可以随着刚体的移动.旋转不断更新,并进行碰 ...
- css3动画效果:3 3D动画
立方体旋转动画效果 css #container{ width: 400px; height: 400px; ; ; -webkit-perspective-origin:50% 225px; per ...
- OpenCASCADE Shape Location
OpenCASCADE Shape Location eryar@163.com Abstract. The TopLoc package of OpenCASCADE gives resources ...
- OpenCascade Shape Representation in OpenSceneGraph
OpenCascade Shape Representation in OpenSceneGraph eryar@163.com 摘要Abstract:本文通过程序实例,将OpenCascade中的拓 ...
随机推荐
- PHP 二维数组去掉重复值并保持原结构
PHP 二维数组去掉重复值并保持原结构 直接上代码,解释很详细 //二维数组去掉重复值 function arrunique($a){ foreach($a[0] as $k => $v){ / ...
- java9新特性-12-集合工厂方法:快速创建只读集合
1.官方Feature 269: Convenience Factory Methods for Collections 2.产生背景 要创建一个只读.不可改变的集合,必须构造和分配它,然后添加元素, ...
- 基于分布式的短文本命题实体识别之----人名识别(python实现)
目前对中文分词精度影响最大的主要是两方面:未登录词的识别和歧义切分. 据统计:未登录词中中文姓人名在文本中一般只占2%左右,但这其中高达50%以上的人名会产生切分错误.在所有的分词错误中,与人名有关的 ...
- java 类和对象10
创建一个Point类,有成员变量x,y,方法getX(),setX(),还有一个构造方法初始化x和y.创建类主类A来测试它. public class Print { private int x; p ...
- IE9+ BUG汇总
CSS透明没有继承 css opacity is not inherited in internet explorer 今儿遇到一个问题源于同事写的一个页面,发现父级明明 opacity:0 了,但子 ...
- #p-complete原来比np更难
转载一下豆瓣的一个不知名的朋友的介绍: NP是指多项式时间内验证其解是否正确.比如: 我们给一个0-1背包的解,就可以在多项式时间内验证是否满足条件.至于是否能找到 满足条件的解,这在NP复杂度里没有 ...
- Spring MVC模式示例(采用解耦控制器)
Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个属性 * @author ...
- 三分钟学会用SpringMVC搭建最小系统(超详细)_转载
前言 做 Java Web 开发的你,一定听说过SpringMVC的大名,作为现在运用最广泛的Java框架,它到目前为止依然保持着强大的活力和广泛的用户群. 本文介绍如何用eclipse一步一步搭建S ...
- react 中间件相关的一些源码解析
零.随便说说中间件 在react的使用中,我们可以将数据放到redux,甚至将一些数据相关的业务逻辑放到redux,这样可以简化我们组件,也更方便组件抽离.封装.复用,只是redux不能很好的处理异步 ...
- bzoj1293 [SCOI2009] 生日礼物 队列
题目描述 小西有一条很长的彩带,彩带上挂着各式各样的彩珠.已知彩珠有N个,分为K种.简单的说,可以将彩带考虑为x轴,每一个彩珠有一个对应的坐标(即位置).某些坐标上可以没有彩珠,但多个彩珠也可以出现在 ...