@@ -537,146 +537,74 @@ public static void MapNormalVectorBuffer(RenderingBufferStorage buffer, XYZ orig
537537 }
538538
539539 /// <summary>
540- /// Maps a buffer to represent normal vectors originating from multiple points in 3D space.
540+ /// Configures the specified <see cref="RenderingBufferStorage"/> instance to represent a rectangular buffer defined
541+ /// by the given minimum and maximum points in 3D space.
541542 /// </summary>
542- /// <remarks>This method generates a set of line segments, where each line represents a normal vector
543- /// originating from a point in the <paramref name="points"/> collection. The direction and length of each normal
544- /// vector are determined by the <paramref name="direction"/> and <paramref name="length"/> parameters,
545- /// respectively. The resulting vertex and index data are stored in the provided <paramref name="buffer"/> for
546- /// rendering.</remarks>
547- /// <param name="buffer">The <see cref="RenderingBufferStorage"/> instance where the vertex and index data for the normal vectors will be
548- /// stored.</param>
549- /// <param name="points">A collection of <see cref="XYZ"/> points representing the starting positions of the normal vectors.</param>
550- /// <param name="direction">The <see cref="XYZ"/> direction vector that determines the orientation of the normal vectors. This vector will
551- /// be normalized.</param>
552- /// <param name="length">The length of each normal vector. Must be a positive value.</param>
553- public static void MapNormalVectorBufferForMultiplePoints ( RenderingBufferStorage buffer , IList < XYZ > points , XYZ direction , double length )
543+ /// <remarks>This method calculates the vertices and indices required to represent a rectangular buffer in
544+ /// 3D space based on the provided <paramref name="min"/> and <paramref name="max"/> points. The buffer is
545+ /// configured with four vertices and two triangles, and the vertex and index buffers are populated accordingly.
546+ /// The orientation of the rectangle is determined by the direction of the vector from <paramref name="min"/> to
547+ /// <paramref name="max"/>. The method adjusts the rectangle's dimensions and orientation based on whether the
548+ /// vector aligns with the X, Y, or Z axis. The caller is responsible for ensuring that the <paramref
549+ /// name="buffer"/> instance is properly initialized before calling this method.</remarks>
550+ /// <param name="buffer">The <see cref="RenderingBufferStorage"/> instance to be configured. This parameter cannot be null.</param>
551+ /// <param name="min">The minimum point of the rectangular buffer in 3D space.</param>
552+ /// <param name="max">The maximum point of the rectangular buffer in 3D space.</param>
553+ public static void MapSideBuffer ( RenderingBufferStorage buffer , XYZ min , XYZ max )
554554 {
555- if ( points . Count == 0 ) return ;
556-
557- var totalLineCount = points . Count ;
558- buffer . VertexBufferCount = totalLineCount * 2 ; // 2 points per line
559- buffer . PrimitiveCount = totalLineCount ;
560-
561- var vertexBufferSizeInFloats = VertexPosition . GetSizeInFloats ( ) * buffer . VertexBufferCount ;
562- buffer . FormatBits = VertexFormatBits . Position ;
563- buffer . VertexBuffer = new VertexBuffer ( vertexBufferSizeInFloats ) ;
564- buffer . VertexBuffer . Map ( vertexBufferSizeInFloats ) ;
565-
566- var vertexStream = buffer . VertexBuffer . GetVertexStreamPosition ( ) ;
567-
568- // Add all start and end points for each normal vector
569- foreach ( var point in points )
555+ var vertexCount = 4 ;
556+ var normal = ( max - min ) . Normalize ( ) ;
557+ var length = ( max - min ) . GetLength ( ) / 2 ;
558+
559+ XYZ point1 ;
560+ XYZ point2 ;
561+ XYZ point3 ;
562+ XYZ point4 ;
563+ if ( normal . IsAlmostEqualTo ( XYZ . BasisX ) )
570564 {
571- var normalizedDirection = direction . Normalize ( ) ;
572- var endPoint = point + normalizedDirection * length ;
573-
574- vertexStream . AddVertex ( new VertexPosition ( point ) ) ;
575- vertexStream . AddVertex ( new VertexPosition ( endPoint ) ) ;
565+ point1 = new XYZ ( min . X , min . Y - length , min . Z ) ;
566+ point2 = new XYZ ( min . X , min . Y + length , min . Z ) ;
567+ point3 = new XYZ ( max . X , max . Y - length , max . Z ) ;
568+ point4 = new XYZ ( max . X , max . Y + length , max . Z ) ;
576569 }
577-
578- buffer . VertexBuffer . Unmap ( ) ;
579-
580- // Create index buffer for lines
581- buffer . IndexBufferCount = totalLineCount * IndexLine . GetSizeInShortInts ( ) ;
582- buffer . IndexBuffer = new IndexBuffer ( buffer . IndexBufferCount ) ;
583- buffer . IndexBuffer . Map ( buffer . IndexBufferCount ) ;
584-
585- var indexStream = buffer . IndexBuffer . GetIndexStreamLine ( ) ;
586-
587- // Add lines connecting start and end points
588- for ( var i = 0 ; i < totalLineCount ; i ++ )
570+ else if ( normal . IsAlmostEqualTo ( XYZ . BasisY ) )
589571 {
590- indexStream . AddLine ( new IndexLine ( i * 2 , i * 2 + 1 ) ) ;
572+ point1 = new XYZ ( min . X , min . Y , min . Z - length ) ;
573+ point2 = new XYZ ( min . X , min . Y , min . Z + length ) ;
574+ point3 = new XYZ ( max . X , max . Y , max . Z - length ) ;
575+ point4 = new XYZ ( max . X , max . Y , max . Z + length ) ;
591576 }
592-
593- buffer . IndexBuffer . Unmap ( ) ;
594- buffer . VertexFormat = new VertexFormat ( buffer . FormatBits ) ;
595- }
596-
597- /// <summary>
598- /// Maps a rendering buffer to represent a series of quads, each centered at a specified point, with a given normal
599- /// and axis-aligned dimensions.
600- /// </summary>
601- /// <remarks>This method generates vertex and index data for a series of quads, where each quad is
602- /// centered at a point in the <paramref name="points"/> collection. The quads are oriented based on the specified
603- /// <paramref name="normal"/> vector, and their size is determined by the <paramref name="axisLength"/> parameter.
604- /// The resulting data is stored in the provided <paramref name="buffer"/> for rendering purposes. The method
605- /// ensures that the buffer is properly mapped and populated with the required vertex and index data. Each quad is
606- /// represented by four vertices and two triangles, resulting in a total of eight indices per quad. If the
607- /// <paramref name="points"/> collection is empty, the method returns immediately without modifying the
608- /// buffer.</remarks>
609- /// <param name="buffer">The <see cref="RenderingBufferStorage"/> instance to be populated with vertex and index data for the quads.</param>
610- /// <param name="points">A collection of <see cref="XYZ"/> points, where each point represents the center of a quad.</param>
611- /// <param name="normal">The normal vector defining the orientation of the quads. This vector is used to calculate the plane on which
612- /// the quads lie.</param>
613- /// <param name="axisLength">The length of the axes used to define the size of each quad. Each quad will have sides proportional to twice
614- /// this length.</param>
615- public static void MapSideBufferForMultiplePoints ( RenderingBufferStorage buffer , IList < XYZ > points , XYZ normal , double axisLength )
616- {
617- if ( points . Count == 0 ) return ;
618-
619- var totalQuadCount = points . Count ;
620- buffer . VertexBufferCount = totalQuadCount * 4 ; // 4 corners per quad
621- buffer . PrimitiveCount = totalQuadCount * 2 ; // 2 triangles per quad
622-
577+ else
578+ {
579+ point1 = new XYZ ( min . X - length , min . Y , min . Z ) ;
580+ point2 = new XYZ ( min . X + length , min . Y , min . Z ) ;
581+ point3 = new XYZ ( max . X - length , max . Y , max . Z ) ;
582+ point4 = new XYZ ( max . X + length , max . Y , max . Z ) ;
583+ }
584+
585+ buffer . VertexBufferCount = vertexCount ;
586+ buffer . PrimitiveCount = 2 ;
587+
623588 var vertexBufferSizeInFloats = VertexPosition . GetSizeInFloats ( ) * buffer . VertexBufferCount ;
624589 buffer . FormatBits = VertexFormatBits . Position ;
625590 buffer . VertexBuffer = new VertexBuffer ( vertexBufferSizeInFloats ) ;
626591 buffer . VertexBuffer . Map ( vertexBufferSizeInFloats ) ;
627-
592+
628593 var vertexStream = buffer . VertexBuffer . GetVertexStreamPosition ( ) ;
629-
630- // Create quads for each point
631- foreach ( var point in points )
632- {
633- var normalizedNormal = normal . Normalize ( ) ;
634-
635- // Calculate perpendicular vectors to create a plane
636- var perpendicular1 = Math . Abs ( normalizedNormal . Z ) < 0.9
637- ? XYZ . BasisZ . CrossProduct ( normalizedNormal ) . Normalize ( )
638- : XYZ . BasisX . CrossProduct ( normalizedNormal ) . Normalize ( ) ;
639-
640- var perpendicular2 = normalizedNormal . CrossProduct ( perpendicular1 ) . Normalize ( ) ;
641-
642- // Scale perpendicular vectors
643- perpendicular1 *= axisLength ;
644- perpendicular2 *= axisLength ;
645-
646- // Create quad corners
647- var corner1 = point + perpendicular1 + perpendicular2 ;
648- var corner2 = point + perpendicular1 - perpendicular2 ;
649- var corner3 = point - perpendicular1 - perpendicular2 ;
650- var corner4 = point - perpendicular1 + perpendicular2 ;
651-
652- // Add vertices
653- vertexStream . AddVertex ( new VertexPosition ( corner1 ) ) ;
654- vertexStream . AddVertex ( new VertexPosition ( corner2 ) ) ;
655- vertexStream . AddVertex ( new VertexPosition ( corner3 ) ) ;
656- vertexStream . AddVertex ( new VertexPosition ( corner4 ) ) ;
657- }
658-
594+ vertexStream . AddVertex ( new VertexPosition ( point1 ) ) ;
595+ vertexStream . AddVertex ( new VertexPosition ( point2 ) ) ;
596+ vertexStream . AddVertex ( new VertexPosition ( point3 ) ) ;
597+ vertexStream . AddVertex ( new VertexPosition ( point4 ) ) ;
598+
659599 buffer . VertexBuffer . Unmap ( ) ;
660-
661- // Create index buffer for triangles
662- buffer . IndexBufferCount = totalQuadCount * 2 * IndexTriangle . GetSizeInShortInts ( ) ;
600+ buffer . IndexBufferCount = 2 * IndexTriangle . GetSizeInShortInts ( ) ;
663601 buffer . IndexBuffer = new IndexBuffer ( buffer . IndexBufferCount ) ;
664602 buffer . IndexBuffer . Map ( buffer . IndexBufferCount ) ;
665-
603+
666604 var indexStream = buffer . IndexBuffer . GetIndexStreamTriangle ( ) ;
667-
668- // Add triangles for each quad
669- for ( var i = 0 ; i < totalQuadCount ; i ++ )
670- {
671- var baseIndex = i * 4 ;
672-
673- // First triangle
674- indexStream . AddTriangle ( new IndexTriangle ( baseIndex , baseIndex + 1 , baseIndex + 2 ) ) ;
675-
676- // Second triangle
677- indexStream . AddTriangle ( new IndexTriangle ( baseIndex , baseIndex + 2 , baseIndex + 3 ) ) ;
678- }
679-
605+ indexStream . AddTriangle ( new IndexTriangle ( 0 , 1 , 2 ) ) ;
606+ indexStream . AddTriangle ( new IndexTriangle ( 1 , 2 , 3 ) ) ;
607+
680608 buffer . IndexBuffer . Unmap ( ) ;
681609 buffer . VertexFormat = new VertexFormat ( buffer . FormatBits ) ;
682610 }
0 commit comments