首页 > 解决方案 > 创建的边界框的深度不正确

问题描述

我的应用程序用手势创建了一个绘图,然后我想在绘图周围创建一个边界框。我已经尝试过默认的苹果 boundingBox 的方法,但该方法总是太大,所以我尝试实现自己的方法。

目前我使用当前函数创建了我的盒子:

                    let nodes: [SCNNode] = getMyNodes()
                    var minposition: SCNVector3 = SCNVector3()
                    var maxpostion: SCNVector3 = SCNVector3()
                    var node_count = 0

                    for node in nodes {
                        if(sceneView.anchor(for: node)?.name != nil) && (sceneView.anchor(for: node)?.name != "dot"){
                            parentNode.addChildNode(node)
                            print(node)
                            if node_count == 0 {
                                minposition = SCNVector3((node.worldPosition.x), (node.worldPosition.y),
                                                         (node.worldPosition.z))
                                maxpostion = SCNVector3((node.worldPosition.x), (node.worldPosition.y),
                                                         (node.worldPosition.z))
                            } else{
                            minposition = SCNVector3(min(node.worldPosition.x, minposition.x), min(node.worldPosition.y, minposition.y), min(node.worldPosition.z, minposition.z))
                            maxpostion = SCNVector3(max(node.worldPosition.x, maxpostion.x), max(node.worldPosition.y, maxpostion.y), max(node.worldPosition.z, maxpostion.z))
                            }
                            node_count += 1
                        }
                    }
                    let width = sqrt((minposition.x - maxpostion.x) * (minposition.x - maxpostion.x))
                    let height = sqrt((minposition.y - maxpostion.y) * (minposition.y - maxpostion.y))
                    let length = sqrt((minposition.z - maxpostion.z) * (minposition.z - maxpostion.z))
                    let box = SCNBox(width: CGFloat(width), height: CGFloat(height), length: CGFloat(length), chamferRadius: 0)
                    let boxNode = SCNNode(geometry: box)
                    let sm = "float u = _surface.diffuseTexcoord.x; \n" +
                        "float v = _surface.diffuseTexcoord.y; \n" +
                        "int u100 = int(u * 100); \n" +
                        "int v100 = int(v * 100); \n" +
                        "if (u100 % 99 == 0 || v100 % 99 == 0) { \n" +
                        "  // do nothing \n" +
                        "} else { \n" +
                        "    discard_fragment(); \n" +
                        "} \n"
                    box.firstMaterial?.diffuse.contents = UIColor.white
//                    box.firstMaterial?.transparency = 0.4
                    box.firstMaterial?.shaderModifiers = [SCNShaderModifierEntryPoint.surface: sm]
                    box.firstMaterial?.isDoubleSided = true
                    boxNode.worldPosition =  SCNVector3((minposition.x + maxpostion.x)/2, (minposition.y + maxpostion.y)/2, (minposition.z + maxpostion.z)/2)
                    boxNode.name = "box"
                    boxNode.addChildNode(parentNode)
                    sceneView.scene.rootNode.addChildNode(boxNode)
                    boundingBox = boxNode

盒子的创建起初看起来不错,但是一旦手机四处移动,它显然没有正确地封装代码。我认为z坐标是错误的。我提供了以下视频链接:

https://res.cloudinary.com/df7kpyhrg/video/upload/v1632127635/RPReplay_Final1632126590_ut4kvs.mp4

我已经在控制台中打印了部分节点位置。还有更多节点,我只包括2个作为示例。

<SCNNode: 0x283704f00 pos(0.000252 0.001159 0.000059) rot(-0.997185 -0.009238 -0.074483 0.205711) scale(1.000000 1.000000 1.000000) | camera=<SCNCamera: 0x105210440> | no child>
<SCNNode: 0x283702b00 | light=<SCNLight: 0x105406a70 | type=probe> | no child>
<SCNNode: 0x283765600 'box' pos(-0.004783 -0.003066 -0.383987) | geometry=<SCNBox: 0x283238620 | width=0.068 height=0.077 length=0.021 chamferRadius=0.000> | 1 child>
<SCNNode: 0x283700b00 | 27 children>
<SCNNode: 0x28375d200 pos(-0.013036 -0.012685 -0.374262) | 1 child>
<SCNNode: 0x28375d800 pos(0.002972 0.000524 0.000786) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283742100 pos(-0.010064 -0.012161 -0.373476) | 65 children>
<SCNNode: 0x283741c00 | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283743c00 pos(-0.000203 -0.000448 -0.000089) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283743d00 pos(-0.000405 -0.000897 -0.000179) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283743e00 pos(-0.000608 -0.001345 -0.000268) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283743f00 pos(-0.000810 -0.001793 -0.000358) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756d00 pos(-0.001013 -0.002241 -0.000447) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756800 pos(-0.001215 -0.002690 -0.000537) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755b00 pos(-0.001418 -0.003138 -0.000626) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755c00 pos(-0.001620 -0.003586 -0.000716) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756700 pos(-0.001823 -0.004035 -0.000805) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756900 pos(-0.002026 -0.004483 -0.000895) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756000 pos(-0.002228 -0.004931 -0.000984) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755f00 pos(-0.002431 -0.005379 -0.001074) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756300 pos(-0.002633 -0.005828 -0.001163) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283756400 pos(-0.002836 -0.006276 -0.001253) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755700 pos(-0.003038 -0.006724 -0.001342) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755900 pos(-0.003241 -0.007173 -0.001432) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755800 pos(-0.003444 -0.007621 -0.001521) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283755600 pos(-0.003646 -0.008069 -0.001611) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28372e300 pos(-0.003849 -0.008517 -0.001700) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28372ea00 pos(-0.004051 -0.008966 -0.001790) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283729500 pos(-0.004254 -0.009414 -0.001879) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283728300 pos(-0.004456 -0.009862 -0.001969) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283727c00 pos(-0.004659 -0.010311 -0.002058) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28373e500 pos(-0.004861 -0.010759 -0.002148) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28373ee00 pos(-0.005064 -0.011207 -0.002237) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370fa00 pos(-0.005267 -0.011655 -0.002327) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370fd00 pos(-0.005469 -0.012104 -0.002416) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f900 pos(-0.005672 -0.012552 -0.002506) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f800 pos(-0.005874 -0.013000 -0.002595) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f600 pos(-0.006077 -0.013449 -0.002685) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f700 pos(-0.006279 -0.013897 -0.002774) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f500 pos(-0.006482 -0.014345 -0.002864) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28370f400 pos(-0.006684 -0.014793 -0.002953) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283715100 pos(-0.006887 -0.015242 -0.003043) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283715500 pos(-0.007090 -0.015690 -0.003132) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716900 pos(-0.007292 -0.016138 -0.003222) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283715a00 pos(-0.007495 -0.016587 -0.003311) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283714a00 pos(-0.007697 -0.017035 -0.003401) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716500 pos(-0.007900 -0.017483 -0.003490) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716b00 pos(-0.008102 -0.017931 -0.003580) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716400 pos(-0.008305 -0.018380 -0.003669) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716300 pos(-0.008507 -0.018828 -0.003759) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283714300 pos(-0.008710 -0.019276 -0.003848) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283716200 pos(-0.008913 -0.019725 -0.003938) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283711800 pos(-0.009115 -0.020173 -0.004027) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283710400 pos(-0.009318 -0.020621 -0.004117) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283712900 pos(-0.009520 -0.021069 -0.004206) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283710600 pos(-0.009723 -0.021518 -0.004295) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283712a00 pos(-0.009925 -0.021966 -0.004385) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28375bc00 pos(-0.010128 -0.022414 -0.004474) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x28375b900 pos(-0.010331 -0.022863 -0.004564) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744000 pos(-0.010533 -0.023311 -0.004653) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744100 pos(-0.010736 -0.023759 -0.004743) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744200 pos(-0.010938 -0.024208 -0.004832) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744300 pos(-0.011141 -0.024656 -0.004922) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744400 pos(-0.011343 -0.025104 -0.005011) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744500 pos(-0.011546 -0.025552 -0.005101) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744600 pos(-0.011748 -0.026001 -0.005190) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744700 pos(-0.011951 -0.026449 -0.005280) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744800 pos(-0.012154 -0.026897 -0.005369) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744900 pos(-0.012356 -0.027346 -0.005459) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744a00 pos(-0.012559 -0.027794 -0.005548) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744b00 pos(-0.012761 -0.028242 -0.005638) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744c00 pos(-0.012964 -0.028690 -0.005727) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283744d00 pos(-0.023432 -0.041745 -0.379382) | 42 children>
<SCNNode: 0x283745200 | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745000 pos(-0.000340 0.000367 0.000015) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745300 pos(-0.000680 0.000733 0.000029) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745400 pos(-0.001019 0.001100 0.000044) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745500 pos(-0.001359 0.001466 0.000058) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745600 pos(-0.001699 0.001833 0.000073) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745700 pos(-0.002039 0.002199 0.000087) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745800 pos(-0.002378 0.002566 0.000102) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745900 pos(-0.002718 0.002932 0.000116) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745a00 pos(-0.003058 0.003299 0.000131) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745b00 pos(-0.003398 0.003665 0.000145) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745c00 pos(-0.003737 0.004032 0.000160) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745d00 pos(-0.004077 0.004399 0.000174) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745e00 pos(-0.004417 0.004765 0.000189) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283745f00 pos(-0.004757 0.005132 0.000203) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746000 pos(-0.005096 0.005498 0.000218) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746100 pos(-0.005436 0.005865 0.000232) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746200 pos(-0.005776 0.006231 0.000247) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746300 pos(-0.006116 0.006598 0.000262) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746400 pos(-0.006455 0.006964 0.000276) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746500 pos(-0.006795 0.007331 0.000291) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746600 pos(-0.007135 0.007698 0.000305) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746700 pos(-0.007475 0.008064 0.000320) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746800 pos(-0.007814 0.008431 0.000334) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746900 pos(-0.008154 0.008797 0.000349) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746a00 pos(-0.008494 0.009164 0.000363) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746b00 pos(-0.008834 0.009530 0.000378) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746c00 pos(-0.009173 0.009897 0.000392) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746d00 pos(-0.009513 0.010263 0.000407) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746e00 pos(-0.009853 0.010630 0.000421) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283746f00 pos(-0.010193 0.010996 0.000436) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747000 pos(-0.010532 0.011363 0.000450) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747100 pos(-0.010872 0.011730 0.000465) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747200 pos(-0.011212 0.012096 0.000479) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747300 pos(-0.011552 0.012463 0.000494) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747400 pos(-0.011891 0.012829 0.000508) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747500 pos(-0.012231 0.013196 0.000523) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747600 pos(-0.012571 0.013562 0.000538) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747700 pos(-0.012911 0.013929 0.000552) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747800 pos(-0.013250 0.014295 0.000567) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747900 pos(-0.013590 0.014662 0.000581) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>
<SCNNode: 0x283747a00 pos(-0.013930 0.015028 0.000596) | geometry=<SCNSphere: 0x283e1ff00 | radius=0.004> | no child>

我检查了所有节点的欧拉角、旋转和相机属性,它们看起来都一样。我不确定我做错了什么。

标签: swiftscenekit

解决方案


推荐阅读