package d2isometricimg

import (
	
	
	
)

// Paths split at structural branches so each dash keeps its own phase. Their
// butt ends need one shared exterior join where a silhouette turns a corner.
// Only ends of painted ribbon parts participate; a dash gap stays open.
func ( *meshBuilder) (,  []classicInkSegment,  float64,  *Material) {
	if .err != nil ||  <= 0 {
		return
	}
	 := make(map[classicInkKey]int)
	for ,  := range  {
		[classicInkKeyOf(.a)]++
		[classicInkKeyOf(.c)]++
	}
	 := make(map[classicInkKey][]classicInkSegment)
	for ,  := range  {
		 := classicInkKeyOf(.a)
		if [] > 2 {
			[] = append([], )
		}
	}
	 := make([]classicInkKey, 0, len())
	for  := range  {
		 = append(, )
	}
	sort.Slice(, func(,  int) bool { return classicInkLess([], []) })
	 := nativeCameraAxes()
	 := .direction
	type  struct {
		 Vec
		     float64
	}
	for ,  := range  {
		if  := .ctx.Err();  != nil {
			.err = 
			return
		}
		 := []
		if len() < 2 {
			continue
		}
		var  []
		var  []*classicInkFacet
		 := make(map[*classicInkFacet]bool)
		for ,  := range  {
			 := nsub(.c, .a)
			 = nsub(, nmul(, ndot(, )))
			if nlen() < 1e-10 {
				continue
			}
			 := nunit()
			 = append(, {, math.Atan2(ndot(, .up), ndot(, .right))})
			for ,  := range .support {
				if ![] {
					[] = true
					 = append(, )
					if len() > maxClassicInkSupportFaces {
						.err = fmt.Errorf("isometric outline join exceeds %d local supporting faces", maxClassicInkSupportFaces)
						return
					}
				}
			}
		}
		if len() < 2 {
			continue
		}
		sort.Slice(, func(,  int) bool { return []. < []. })
		 := [0].a
		 := func( Vec) Vertex {
			 := nadd(, )
			if math.Abs(.Y) > 1e-8 {
				 = nsub(, nmul(, .Y/.Y))
			}
			return Vertex{Position: , Normal: }
		}
		for ,  := range  {
			 := [(+1)%len()]
			 := . - .
			if  == len()-1 {
				 += 2 * math.Pi
			}
			if  <= math.Pi+1e-8 ||  >= 2*math.Pi-1e-8 {
				continue
			}
			// Smaller sectors are already covered by the adjacent strips.
			// Fill the outer wedge with a sharp miter, or a bounded bevel
			// at acute tips, without extending the authored stroke weight.
			 := ncross(, .)
			 := nmul(ncross(, .), -1)
			,  := nmul(, ), nmul(, )
			 := nmul(nadd(, ), /max(1e-12, 1+ndot(, )))
			if nlen() <= 2* {
				.classicInkFace([3]Vertex{(Vec{}), (), ()}, , , )
				.classicInkFace([3]Vertex{(Vec{}), (), ()}, , , )
			} else {
				.classicInkFace([3]Vertex{(Vec{}), (), ()}, , , )
			}
		}
	}
}