@@ -259,14 +259,14 @@ fn clip_polyline_to_bbox(nodes: &[ProcessedNode], xzbbox: &XZBBox) -> Vec<Proces
259259 // Preserve endpoint IDs where possible
260260 if result. len ( ) >= 2 {
261261 let tolerance = 50.0 ;
262- if let Some ( first_orig) = nodes. first ( ) {
263- if matches_endpoint (
262+ if let Some ( first_orig) = nodes. first ( )
263+ && matches_endpoint (
264264 ( f64:: from ( result[ 0 ] . x ) , f64:: from ( result[ 0 ] . z ) ) ,
265265 first_orig,
266266 tolerance,
267- ) {
268- result [ 0 ] . id = first_orig . id ;
269- }
267+ )
268+ {
269+ result [ 0 ] . id = first_orig . id ;
270270 }
271271 if let Some ( last_orig) = nodes. last ( ) {
272272 let last_idx = result. len ( ) - 1 ;
@@ -320,26 +320,12 @@ fn clip_polygon_sutherland_hodgman(
320320 let next_inside = point_inside_edge ( next, edge_x1, edge_z1, edge_x2, edge_z2) ;
321321
322322 if next_inside {
323- if !current_inside {
324- if let Some ( mut intersection) = line_edge_intersection (
323+ if !current_inside
324+ && let Some ( mut intersection) = line_edge_intersection (
325325 current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
326- ) {
327- // Clamp to current edge only
328- match edge_idx {
329- 0 => intersection. 1 = min_z,
330- 1 => intersection. 0 = max_x,
331- 2 => intersection. 1 = max_z,
332- 3 => intersection. 0 = min_x,
333- _ => { }
334- }
335- clipped. push ( intersection) ;
336- }
337- }
338- clipped. push ( next) ;
339- } else if current_inside {
340- if let Some ( mut intersection) = line_edge_intersection (
341- current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
342- ) {
326+ )
327+ {
328+ // Clamp to current edge only
343329 match edge_idx {
344330 0 => intersection. 1 = min_z,
345331 1 => intersection. 0 = max_x,
@@ -349,6 +335,20 @@ fn clip_polygon_sutherland_hodgman(
349335 }
350336 clipped. push ( intersection) ;
351337 }
338+ clipped. push ( next) ;
339+ } else if current_inside
340+ && let Some ( mut intersection) = line_edge_intersection (
341+ current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
342+ )
343+ {
344+ match edge_idx {
345+ 0 => intersection. 1 = min_z,
346+ 1 => intersection. 0 = max_x,
347+ 2 => intersection. 1 = max_z,
348+ 3 => intersection. 0 = min_x,
349+ _ => { }
350+ }
351+ clipped. push ( intersection) ;
352352 }
353353 }
354354
@@ -388,24 +388,24 @@ fn clip_polygon_sutherland_hodgman_simple(
388388 let next_inside = point_inside_edge ( next, edge_x1, edge_z1, edge_x2, edge_z2) ;
389389
390390 if next_inside {
391- if !current_inside {
392- if let Some ( mut intersection) = line_edge_intersection (
391+ if !current_inside
392+ && let Some ( mut intersection) = line_edge_intersection (
393393 current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
394- ) {
395- intersection. 0 = intersection. 0 . clamp ( min_x, max_x) ;
396- intersection. 1 = intersection. 1 . clamp ( min_z, max_z) ;
397- clipped. push ( intersection) ;
398- }
399- }
400- clipped. push ( next) ;
401- } else if current_inside {
402- if let Some ( mut intersection) = line_edge_intersection (
403- current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
404- ) {
394+ )
395+ {
405396 intersection. 0 = intersection. 0 . clamp ( min_x, max_x) ;
406397 intersection. 1 = intersection. 1 . clamp ( min_z, max_z) ;
407398 clipped. push ( intersection) ;
408399 }
400+ clipped. push ( next) ;
401+ } else if current_inside
402+ && let Some ( mut intersection) = line_edge_intersection (
403+ current. 0 , current. 1 , next. 0 , next. 1 , edge_x1, edge_z1, edge_x2, edge_z2,
404+ )
405+ {
406+ intersection. 0 = intersection. 0 . clamp ( min_x, max_x) ;
407+ intersection. 1 = intersection. 1 . clamp ( min_z, max_z) ;
408+ clipped. push ( intersection) ;
409409 }
410410 }
411411
@@ -632,10 +632,11 @@ fn remove_consecutive_duplicates(polygon: Vec<(f64, f64)>) -> Vec<(f64, f64)> {
632632 let mut result: Vec < ( f64 , f64 ) > = Vec :: with_capacity ( polygon. len ( ) ) ;
633633
634634 for p in & polygon {
635- if let Some ( last) = result. last ( ) {
636- if ( p. 0 - last. 0 ) . abs ( ) < eps && ( p. 1 - last. 1 ) . abs ( ) < eps {
637- continue ;
638- }
635+ if let Some ( last) = result. last ( )
636+ && ( p. 0 - last. 0 ) . abs ( ) < eps
637+ && ( p. 1 - last. 1 ) . abs ( ) < eps
638+ {
639+ continue ;
639640 }
640641 result. push ( * p) ;
641642 }
@@ -682,25 +683,25 @@ fn assign_node_ids_preserving_endpoints(
682683 let is_last = i == last_index;
683684
684685 if is_first || is_last {
685- if let Some ( first) = original_first {
686- if matches_endpoint ( coord, first, tolerance) {
687- return ProcessedNode {
688- id : first . id ,
689- x : coord . 0 . round ( ) as i32 ,
690- z : coord. 1 . round ( ) as i32 ,
691- tags : HashMap :: new ( ) ,
692- } ;
693- }
686+ if let Some ( first) = original_first
687+ && matches_endpoint ( coord, first, tolerance)
688+ {
689+ return ProcessedNode {
690+ id : first . id ,
691+ x : coord. 0 . round ( ) as i32 ,
692+ z : coord . 1 . round ( ) as i32 ,
693+ tags : HashMap :: new ( ) ,
694+ } ;
694695 }
695- if let Some ( last) = original_last {
696- if matches_endpoint ( coord, last, tolerance) {
697- return ProcessedNode {
698- id : last . id ,
699- x : coord . 0 . round ( ) as i32 ,
700- z : coord. 1 . round ( ) as i32 ,
701- tags : HashMap :: new ( ) ,
702- } ;
703- }
696+ if let Some ( last) = original_last
697+ && matches_endpoint ( coord, last, tolerance)
698+ {
699+ return ProcessedNode {
700+ id : last . id ,
701+ x : coord. 0 . round ( ) as i32 ,
702+ z : coord . 1 . round ( ) as i32 ,
703+ tags : HashMap :: new ( ) ,
704+ } ;
704705 }
705706 }
706707
0 commit comments