|
@@ -0,0 +1,298 @@
|
|
|
+import javafx.scene.layout.Pane;
|
|
|
+import javafx.stage.Stage;
|
|
|
+
|
|
|
+import javax.xml.transform.Result;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+public class MeanShift {
|
|
|
+ public ArrayList<Double> pointx;
|
|
|
+ public ArrayList<Double> pointy;
|
|
|
+ public double R;
|
|
|
+ ArrayList<ResultDataType> result = new ArrayList<>();
|
|
|
+ public void setPointxAndPonty(ArrayList<Double> pointx_input,ArrayList<Double> pointy_input){
|
|
|
+ this.pointx = pointx_input;
|
|
|
+ this.pointy = pointy_input;
|
|
|
+ }
|
|
|
+ public void setR(double R_input){
|
|
|
+ this.R = R_input;
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Integer> DL(ArrayList<Integer> NumList){
|
|
|
+ for(int i = 0;i < NumList.size();i++){
|
|
|
+ int temp_index = (int)(Math.random()*(NumList.size() - i)) + i;
|
|
|
+ int temp = NumList.get(i);
|
|
|
+ NumList.set(i,NumList.get(temp_index));
|
|
|
+ NumList.set(temp_index,temp);
|
|
|
+ }
|
|
|
+ return NumList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer PickUpAPointNum(ArrayList<Integer> NumListLeft){
|
|
|
+ NumListLeft = DL(NumListLeft);
|
|
|
+ return NumListLeft.get(0);
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<Integer> FindPointinsideR(double centerx,double centery){
|
|
|
+ ArrayList<Integer> NuminsideR = new ArrayList<>();
|
|
|
+ for(int i = 0 ; i < pointx.size() ; i++){
|
|
|
+ if (CalculateDistance(pointx.get(i),pointy.get(i),centerx,centery) < R){
|
|
|
+ NuminsideR.add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return NuminsideR;
|
|
|
+ }
|
|
|
+
|
|
|
+ public double CalculateDistance(Double x1,Double y1, Double x2 ,Double y2){
|
|
|
+ return Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2));
|
|
|
+ }
|
|
|
+
|
|
|
+ public double[] CalculateMh(ArrayList<Integer> NuminsideR,double centerx,double centery){
|
|
|
+ double Sumx = 0.;
|
|
|
+ double Sumy = 0.;
|
|
|
+ double[] mh = new double[2];
|
|
|
+ for(int i = 0 ; i < NuminsideR.size();i++){
|
|
|
+ Sumx = Sumx + pointx.get(NuminsideR.get(i)) - centerx;
|
|
|
+ Sumy = Sumy + pointy.get(NuminsideR.get(i)) - centery;
|
|
|
+ }
|
|
|
+
|
|
|
+ mh[0] = Sumx/NuminsideR.size();
|
|
|
+ mh[1] = Sumy/NuminsideR.size();
|
|
|
+ return mh;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean judgeDensity(double x1,double y1,double x2,double y2){
|
|
|
+ int N_old = FindPointinsideR(x1,y1).size();
|
|
|
+ int N_new = FindPointinsideR(x2,y2).size();
|
|
|
+ if (N_old >= N_new){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public ArrayList<ResultDataType> MainLoop() throws FileNotFoundException {
|
|
|
+ ArrayList<Integer> numlist = new ArrayList<>();
|
|
|
+ ArrayList<Integer> numlist_used = new ArrayList<>();
|
|
|
+ ArrayList<Integer> numlist_class = new ArrayList<>();
|
|
|
+ ArrayList<Integer> numlist_unused = new ArrayList<>();
|
|
|
+ ArrayList<Integer> numlist_center = new ArrayList<>();
|
|
|
+ ArrayList<Double> centerx_history = new ArrayList<>();
|
|
|
+ ArrayList<Double> centery_history = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
+// ArrayList<Integer> numlist_used_forcal = new ArrayList<>(numlist_used);
|
|
|
+// ArrayList<Integer> numlist_unused_forcal = new ArrayList<>(numlist_unused);
|
|
|
+// ArrayList<Integer> numlist_class_forcal = new ArrayList<>(numlist_class);
|
|
|
+// ArrayList<Integer> numlist_center_forcal = new ArrayList<>(numlist_center);
|
|
|
+// ArrayList<Integer> numlist_forcal = new ArrayList<>(numlist);
|
|
|
+
|
|
|
+
|
|
|
+ for (int i = 0; i < pointx.size() ; i++){
|
|
|
+ numlist.add(i);
|
|
|
+ }
|
|
|
+ numlist_unused = numlist;
|
|
|
+ /////////////////////////////////////////ppppppppppppppppppppppppppp
|
|
|
+// Set<Integer> hashset = new HashSet<>();//?
|
|
|
+ int count_for_outside_loop = 0;
|
|
|
+
|
|
|
+
|
|
|
+ while (!numlist_unused.isEmpty()){
|
|
|
+// System.out.println(numlist_unused);
|
|
|
+// System.out.println("centerx_history is "+centerx_history);
|
|
|
+// System.out.println("centery_history is "+centery_history);
|
|
|
+ count_for_outside_loop = count_for_outside_loop + 1;
|
|
|
+ int num = PickUpAPointNum(numlist_unused);
|
|
|
+ ArrayList<Integer> NuminsideR = new ArrayList<>();
|
|
|
+ double centerx = pointx.get(num);
|
|
|
+ double centery = pointy.get(num);
|
|
|
+ int inital_num = num;
|
|
|
+ System.out.println("the NO. " + count_for_outside_loop + " outside loop and num picked is " + inital_num);
|
|
|
+ double inital_centerx = centerx;
|
|
|
+ double inital_centery = centery;
|
|
|
+ double centerx_next_test ;
|
|
|
+ double centery_next_test ;
|
|
|
+ int count_for_inside_loop = 0;
|
|
|
+ while(true){
|
|
|
+ count_for_inside_loop = count_for_inside_loop + 1;
|
|
|
+// System.out.println("!!!outside loop is " + count_for_outside_loop);
|
|
|
+// System.out.println("!!!inside loop is " + count_for_inside_loop);
|
|
|
+ NuminsideR = FindPointinsideR(centerx,centery);
|
|
|
+ double[] mh = CalculateMh(NuminsideR,centerx,centery);
|
|
|
+ centerx_next_test = centerx + mh[0];
|
|
|
+ centery_next_test = centery + mh[1];
|
|
|
+ if(!judgeDensity(centerx,centery,centerx_next_test,centery_next_test)){
|
|
|
+ System.out.println("第 " + count_for_outside_loop + " 次外循环的第 " + count_for_inside_loop + " 次漂移");
|
|
|
+ System.out.println("当前圆心为" + centerx + " " + centery);
|
|
|
+ System.out.println("当前圆内点个数为:" + NuminsideR.size());
|
|
|
+ centerx = centerx_next_test;
|
|
|
+ centery = centery_next_test;
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ System.out.println("第 " + count_for_outside_loop + " 次外循环,漂移结束。");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+// NuminsideR = FindPointinsideR(centerx,centery);
|
|
|
+ }
|
|
|
+ int max = 0;
|
|
|
+ int compare_density_count=0;
|
|
|
+ ArrayList<Integer> count_num_cd = new ArrayList<>();
|
|
|
+// System.out.println("head:centerx_history.size() is " + centerx_history.size());
|
|
|
+ for(int i = 0 ; i < centerx_history.size();i++){
|
|
|
+// int max;
|
|
|
+ /////////////
|
|
|
+ if(CalculateDistance(centerx,centery,centerx_history.get(i),centery_history.get(i)) < 2*R){
|
|
|
+ count_num_cd.add(i);
|
|
|
+// System.out.println("count_num_cd.add " + i);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+// System.out.println("centerx and centery is " + centerx + " " + centery);
|
|
|
+// System.out.println("centerx_history and centery_history is " + centerx_history.get(i) + " " + centery_history.get(i));
|
|
|
+// System.out.println("the distance is " + CalculateDistance(centerx,centery,centerx_history.get(i),centery_history.get(i)));
|
|
|
+// System.out.println(" the num in ");
|
|
|
+
|
|
|
+ max += 1;
|
|
|
+ }
|
|
|
+ /////////////
|
|
|
+ }
|
|
|
+// System.out.println("max is" + max);
|
|
|
+ if(max == centerx_history.size()){
|
|
|
+ System.out.println("第 " + count_for_outside_loop + " 次外循环。该循环的圆心和历史收敛圆心距离大于2R。");
|
|
|
+
|
|
|
+// result.add(new ResultDataType(NuminsideR));
|
|
|
+ centerx_history.add(centerx);
|
|
|
+ centery_history.add(centery);
|
|
|
+ numlist_center.add(num);
|
|
|
+ numlist_class.addAll(NuminsideR);
|
|
|
+
|
|
|
+ ArrayList<Integer> numlist_used_forcal = new ArrayList<>(numlist_used);
|
|
|
+ ArrayList<Integer> numlist_unused_forcal = new ArrayList<>(numlist_unused);
|
|
|
+ ArrayList<Integer> numlist_class_forcal = new ArrayList<>(numlist_class);
|
|
|
+ ArrayList<Integer> numlist_center_forcal = new ArrayList<>(numlist_center);
|
|
|
+ ArrayList<Integer> numlist_forcal = new ArrayList<>(numlist);
|
|
|
+
|
|
|
+ numlist_class_forcal.removeAll(numlist_center_forcal);
|
|
|
+ numlist_center_forcal.addAll(numlist_center_forcal);
|
|
|
+
|
|
|
+ numlist_used_forcal = numlist_center_forcal;
|
|
|
+ numlist_forcal.removeAll(numlist_used_forcal);
|
|
|
+ numlist_unused_forcal = numlist_forcal;
|
|
|
+ numlist_unused = numlist_unused_forcal;
|
|
|
+// numlist_unused.remove(inital_num);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ ArrayList<Integer> cd_NuminsideR = new ArrayList<>();
|
|
|
+ System.out.println("第 " + count_for_outside_loop + " 次外循环。当前收敛的圆和历史部分圆重叠。");
|
|
|
+ System.out.println("重叠了 " + count_num_cd.size() + " 个");
|
|
|
+
|
|
|
+ for (int i = 0 ; i < count_num_cd.size(); i++){
|
|
|
+ int length = FindPointinsideR(centerx_history.get(count_num_cd.get(i)),centery_history.get(count_num_cd.get(i))).size();
|
|
|
+ cd_NuminsideR.add(length);
|
|
|
+ }
|
|
|
+ int temp_for_count_num_cd = count_num_cd.size();
|
|
|
+ ArrayList<Double> centerx_history_to_disappear = new ArrayList<>();
|
|
|
+ ArrayList<Double> centery_history_to_disappear = new ArrayList<>();
|
|
|
+ for(int i = 0 ; i < temp_for_count_num_cd ; i++){
|
|
|
+ if(NuminsideR.size() > cd_NuminsideR.get(i)){
|
|
|
+// ArrayList<Integer> centerx_history_forcal = new ArrayList<>(centerx_history);
|
|
|
+// System.out.println("NuminsideR.size() is " + NuminsideR.size());
|
|
|
+// System.out.println("cd_NuminsideR.get(i) is " + cd_NuminsideR.get(i));
|
|
|
+ compare_density_count = compare_density_count + 1;
|
|
|
+
|
|
|
+// System.out.println("i is " + i);
|
|
|
+// System.out.println("count_num_cd.get(i) is " + count_num_cd.get(i) );
|
|
|
+// System.out.println("tail:centerx_history.size() is " + centerx_history.size());
|
|
|
+// System.out.println("tail:centerx_history is " + centerx_history);
|
|
|
+// System.out.println("centerx_history.get(count_num_cd.get(i)) " + centerx_history.get(count_num_cd.get(i)));
|
|
|
+// System.out.println("centerx_history.get(count_num_cd.get(i)) is " +centerx_history.get(count_num_cd.get(i)));
|
|
|
+
|
|
|
+ ArrayList<Integer> the_circle_disappear= FindPointinsideR(centerx_history.get(count_num_cd.get(i)),centery_history.get(count_num_cd.get(i)));
|
|
|
+ centerx_history_to_disappear.add(centerx_history.get(count_num_cd.get(i)));
|
|
|
+ centery_history_to_disappear.add(centery_history.get(count_num_cd.get(i)));
|
|
|
+ ///////////////////////////////////////////////
|
|
|
+ //p
|
|
|
+// centerx_history.remove((int)count_num_cd.get(i));
|
|
|
+// centery_history.remove((int)count_num_cd.get(i));
|
|
|
+ //p
|
|
|
+ ///////////////////////////////////////////////
|
|
|
+
|
|
|
+ ///////////
|
|
|
+// System.out.println("the i is : " + i);
|
|
|
+// System.out.println("count_num_cd before remove is " + count_num_cd);
|
|
|
+ ////////////////////////////////////
|
|
|
+// count_num_cd.remove(i);
|
|
|
+ ///////////////////////////////////
|
|
|
+// System.out.println("count_num_cd after remove is " + count_num_cd);
|
|
|
+ ////////////
|
|
|
+// System.out.println("for test !!!!!!!!!!" + the_circle_disappear);
|
|
|
+ numlist_class.removeAll((ArrayList<Integer>)the_circle_disappear);
|
|
|
+// numlist_unused.remove(inital_num);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for(int i = 0 ; i < centerx_history_to_disappear.size(); i++){
|
|
|
+ centerx_history.remove(centerx_history_to_disappear.get(i));
|
|
|
+ centery_history.remove(centery_history_to_disappear.get(i));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(compare_density_count == cd_NuminsideR.size()){
|
|
|
+ System.out.println("当前收敛圆比和它重叠的历史收敛的圆密度都大");
|
|
|
+ /////////////////////////////
|
|
|
+// for(int i = 0; i < count_num_cd.size(); i++){
|
|
|
+// ArrayList<Integer> the_circle_disappear= FindPointinsideR(centerx_history.get(count_num_cd.get(i)),centery_history.get(count_num_cd.get(i)));
|
|
|
+// centerx_history.remove((int)count_num_cd.get(i));
|
|
|
+// centery_history.remove((int)count_num_cd.get(i));
|
|
|
+// numlist_class.removeAll(the_circle_disappear);
|
|
|
+// }
|
|
|
+ //////////////////////////////////
|
|
|
+ centerx_history.add(centerx);
|
|
|
+ centery_history.add(centery);
|
|
|
+ numlist_center.add(num);
|
|
|
+ numlist_class.addAll(NuminsideR);
|
|
|
+
|
|
|
+ ArrayList<Integer> numlist_used_forcal = new ArrayList<>(numlist_used);
|
|
|
+ ArrayList<Integer> numlist_unused_forcal = new ArrayList<>(numlist_unused);
|
|
|
+ ArrayList<Integer> numlist_class_forcal = new ArrayList<>(numlist_class);
|
|
|
+ ArrayList<Integer> numlist_center_forcal = new ArrayList<>(numlist_center);
|
|
|
+ ArrayList<Integer> numlist_forcal = new ArrayList<>(numlist);
|
|
|
+
|
|
|
+ numlist_class_forcal.removeAll(numlist_center_forcal);
|
|
|
+ numlist_center_forcal.addAll(numlist_center_forcal);
|
|
|
+ numlist_used_forcal = numlist_center_forcal;
|
|
|
+ numlist_forcal.removeAll(numlist_used_forcal);
|
|
|
+ numlist_unused_forcal = numlist_forcal;
|
|
|
+ numlist_unused = numlist_unused_forcal;
|
|
|
+// numlist_unused.remove(inital_num);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ numlist_unused.remove((Integer)inital_num);
|
|
|
+// System.out.println(numlist_unused);
|
|
|
+ }
|
|
|
+// System.out.println("centerxlist is" + centerx_history);
|
|
|
+// System.out.println("centerylist is" + centery_history);
|
|
|
+///////////////////////////////////////////
|
|
|
+ for(int i = 0 ; i < centerx_history.size();i++){
|
|
|
+ ArrayList<Integer> out_NuminsideR = FindPointinsideR(centerx_history.get(i),centery_history.get(i));
|
|
|
+ result.add(new ResultDataType(out_NuminsideR));
|
|
|
+ }
|
|
|
+ for (int i = 0 ; i < centerx_history.size() - 1; i++){
|
|
|
+ for (int j = i+1 ; j < centerx_history.size(); j++){
|
|
|
+ double temp =CalculateDistance(centerx_history.get(i),centery_history.get(i),centerx_history.get(j),centery_history.get(j));
|
|
|
+ System.out.println("ths distance is " + temp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ java.io.File file3 = new java.io.File("C:\\Users\\87969\\Desktop\\test\\meanshiftLastcircle.txt");
|
|
|
+ PrintWriter TestData_OutPut = new PrintWriter(file3);
|
|
|
+ for(int i = 0; i < centerx_history.size();i++){
|
|
|
+ TestData_OutPut.println(centerx_history.get(i) + " " + centery_history.get(i));
|
|
|
+ }
|
|
|
+ TestData_OutPut.close();
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|