<commit_before>package org.twak.siteplan.anchors;

import java.awt.geom.AffineTransform;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JComponent;
import javax.vecmath.Point2d;

import org.twak.siteplan.campskeleton.Siteplan;
import org.twak.camp.Corner;
import org.twak.camp.Edge;
import org.twak.camp.ui.Bar;
import org.twak.camp.ui.Marker;
import org.twak.camp.ui.PointEditor.BarSelected;
import org.twak.siteplan.campskeleton.Global;
import org.twak.siteplan.campskeleton.Plan;
import org.twak.siteplan.campskeleton.PlanSkeleton;
import org.twak.siteplan.campskeleton.PlanUI;
import org.twak.siteplan.campskeleton.Profile;
import org.twak.siteplan.campskeleton.ProfileMachine;
import org.twak.siteplan.campskeleton.PlanSkeleton.ColumnProperties;
import org.twak.utils.Cache;
import org.twak.utils.LContext;
import org.twak.utils.Line;
import org.twak.utils.MUtils;
import org.twak.utils.Pair;
import org.twak.utils.WeakListener;
import org.twak.utils.collections.ConsecutiveItPairs;
import org.twak.utils.collections.Loop;
import org.twak.utils.collections.LoopL;
import org.twak.utils.collections.Loopable;

/**
 * @author twak
 */
public class NaturalStepShip extends Ship
{
    public String name = "n/step";

    public LoopL<Bar> shape = new LoopL();
    public LoopL<Double> speeds = new LoopL();

    private transient PlanSkeleton skel = null;

    // todo: this should be transient!
    public Cache<ProfileHeight, ProfileMachine> profileCache = new Cache<ProfileHeight, ProfileMachine>() {
        @Override
        // fixme: disabled!
        public ProfileMachine create( ProfileHeight i )
        {
            Profile p = new Profile ( i.profile, AffineTransform.getTranslateInstance( 0, -i.height ), skel.plan );
            // register all new profiles & their anchors with the skeleton
            skel.addingProfileRemapRoot( p, i.global );

            return new ProfileMachine( p, skel.plan.root, 0 ); // profile is always the first
        }


    };

    public NaturalStepShip()
    { this (Siteplan.instance.plan); }
    public NaturalStepShip( Plan plan )
    {
//        Plan plan = CampSkeleton.instance.plan;

        Loop<Bar>shapeLoop = new Loop();
        this.shape.add( shapeLoop );

        Profile
            profile1 = plan.createNewProfile(null),
            profile2 = plan.createNewProfile(null);

        Point2d[] coords = new Point2d[] {
            new Point2d (-200,0), new Point2d (50,0), new Point2d (50,-20), new Point2d (250,-20), new Point2d (250,0), new Point2d (500, 0) };

        Profile[] profs = new Profile[] {
            profile1, profile2, profile2, profile2, profile1
        };

        int i = 0;
        for ( Pair<Point2d, Point2d> pair : new ConsecutiveItPairs<Point2d>( Arrays.asList( coords ) ) )
        {
            Bar b = new Bar (pair.first(), pair.second());
            shapeLoop.append( b );

            plan.profiles.put( b, profs[i++] ); 
        }

        Siteplan.instance.profileListChanged();
        
        Loop<Double> speedLoop = new Loop();
        this.speeds.add(speedLoop);
        speedLoop.append(0.);
        speedLoop.append(-6.);
        speedLoop.append(-3.);
        speedLoop.append(-6.);
        speedLoop.append(0.);
    }

    @Override
    public void clearCache()
    {
        profileCache.cache.clear();
    }

    @Override
    public JComponent getToolInterface(WeakListener refreshAnchors, WeakListener.Changed refreshFeatureListListener, Plan plan)
    {
        return new NaturalStepPanel(this, refreshFeatureListListener);
    }

    @Override
    protected Instance createInstance()
    {
        return new NaturalInstance();
    }

    /**
     * Synchronizes the scale between the ui prievew and here
     */
    public static AffineTransform getScaleTransform()
    {
        return AffineTransform.getScaleInstance( 0.001, 0.001 ); // 19/3/2010 was 0.01. added clamp, below
    }

    public class NaturalInstance extends Ship.Instance
    {
        @Override
        public LContext<Corner> process(Anchor anchor, final LContext<Corner> toEdit, Marker planMarker, Marker profileMarker, Edge edge, final AnchorHauler.AnchorHeightEvent hauler, Corner oldLeadingCorner)
        {
            skel = hauler.skel; // the profile cache needs a plan reference
            Corner insertAfter = toEdit.get();
            Corner insertBefore = toEdit.get().nextC;
            final double height = -profileMarker.y;


            // we clamp the plan marker to be a certain distance from the start or end (19/3/2010)
            Line l = new Line ( new Point2d ( insertAfter.x, insertAfter.y), new Point2d ( insertBefore.x, insertBefore.y ) );
            double tol = 1./l.length(); // <-- need to find some science behind this (clamp input size of natural step?)
            Point2d clampedPlanMarker = l.fromPPram( MUtils.clamp( l.findPPram( planMarker ), tol, 1-tol ) );<commit_msg>mathz<commit_after>package org.twak.siteplan.anchors;

import java.awt.geom.AffineTransform;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import javax.swing.JComponent;
import javax.vecmath.Point2d;

import org.twak.siteplan.campskeleton.Siteplan;
import org.twak.camp.Corner;
import org.twak.camp.Edge;
import org.twak.camp.ui.Bar;
import org.twak.camp.ui.Marker;
import org.twak.camp.ui.PointEditor.BarSelected;
import org.twak.siteplan.campskeleton.Global;
import org.twak.siteplan.campskeleton.Plan;
import org.twak.siteplan.campskeleton.PlanSkeleton;
import org.twak.siteplan.campskeleton.PlanUI;
import org.twak.siteplan.campskeleton.Profile;
import org.twak.siteplan.campskeleton.ProfileMachine;
import org.twak.siteplan.campskeleton.PlanSkeleton.ColumnProperties;
import org.twak.utils.Cache;
import org.twak.utils.LContext;
import org.twak.utils.Line;
import org.twak.utils.Mathz;
import org.twak.utils.Pair;
import org.twak.utils.WeakListener;
import org.twak.utils.collections.ConsecutiveItPairs;
import org.twak.utils.collections.Loop;
import org.twak.utils.collections.LoopL;
import org.twak.utils.collections.Loopable;

/**
 * @author twak
 */
public class NaturalStepShip extends Ship
{
    public String name = "n/step";

    public LoopL<Bar> shape = new LoopL();
    public LoopL<Double> speeds = new LoopL();

    private transient PlanSkeleton skel = null;

    // todo: this should be transient!
    public Cache<ProfileHeight, ProfileMachine> profileCache = new Cache<ProfileHeight, ProfileMachine>() {
        @Override
        // fixme: disabled!
        public ProfileMachine create( ProfileHeight i )
        {
            Profile p = new Profile ( i.profile, AffineTransform.getTranslateInstance( 0, -i.height ), skel.plan );
            // register all new profiles & their anchors with the skeleton
            skel.addingProfileRemapRoot( p, i.global );

            return new ProfileMachine( p, skel.plan.root, 0 ); // profile is always the first
        }


    };

    public NaturalStepShip()
    { this (Siteplan.instance.plan); }
    public NaturalStepShip( Plan plan )
    {
//        Plan plan = CampSkeleton.instance.plan;

        Loop<Bar>shapeLoop = new Loop();
        this.shape.add( shapeLoop );

        Profile
            profile1 = plan.createNewProfile(null),
            profile2 = plan.createNewProfile(null);

        Point2d[] coords = new Point2d[] {
            new Point2d (-200,0), new Point2d (50,0), new Point2d (50,-20), new Point2d (250,-20), new Point2d (250,0), new Point2d (500, 0) };

        Profile[] profs = new Profile[] {
            profile1, profile2, profile2, profile2, profile1
        };

        int i = 0;
        for ( Pair<Point2d, Point2d> pair : new ConsecutiveItPairs<Point2d>( Arrays.asList( coords ) ) )
        {
            Bar b = new Bar (pair.first(), pair.second());
            shapeLoop.append( b );

            plan.profiles.put( b, profs[i++] ); 
        }

        Siteplan.instance.profileListChanged();
        
        Loop<Double> speedLoop = new Loop();
        this.speeds.add(speedLoop);
        speedLoop.append(0.);
        speedLoop.append(-6.);
        speedLoop.append(-3.);
        speedLoop.append(-6.);
        speedLoop.append(0.);
    }

    @Override
    public void clearCache()
    {
        profileCache.cache.clear();
    }

    @Override
    public JComponent getToolInterface(WeakListener refreshAnchors, WeakListener.Changed refreshFeatureListListener, Plan plan)
    {
        return new NaturalStepPanel(this, refreshFeatureListListener);
    }

    @Override
    protected Instance createInstance()
    {
        return new NaturalInstance();
    }

    /**
     * Synchronizes the scale between the ui prievew and here
     */
    public static AffineTransform getScaleTransform()
    {
        return AffineTransform.getScaleInstance( 0.001, 0.001 ); // 19/3/2010 was 0.01. added clamp, below
    }

    public class NaturalInstance extends Ship.Instance
    {
        @Override
        public LContext<Corner> process(Anchor anchor, final LContext<Corner> toEdit, Marker planMarker, Marker profileMarker, Edge edge, final AnchorHauler.AnchorHeightEvent hauler, Corner oldLeadingCorner)
        {
            skel = hauler.skel; // the profile cache needs a plan reference
            Corner insertAfter = toEdit.get();
            Corner insertBefore = toEdit.get().nextC;
            final double height = -profileMarker.y;


            // we clamp the plan marker to be a certain distance from the start or end (19/3/2010)
            Line l = new Line ( new Point2d ( insertAfter.x, insertAfter.y), new Point2d ( insertBefore.x, insertBefore.y ) );
            double tol = 1./l.length(); // <-- need to find some science behind this (clamp input size of natural step?)
            Point2d clampedPlanMarker = l.fromPPram( Mathz.clamp( l.findPPram( planMarker ), tol, 1-tol ) );
{'fraction_non_alphanumeric': 0.10756166599272139, 'fraction_numerical': 0.016376870198139912, 'mean_word_length': 2.81527188584651, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1686072', 'n_tokens_mistral': 3118, 'n_tokens_neox': 2939, 'n_words': 734}