// File: /workspace/github_cpp_makefile_star2_O2/jingdao[P]PointCloudApp/descriptor.cpp
// Function: Descriptor::kMeansClustering(std::vector<std::vector<int, std::allocator<int>>, std::allocator<std::vector<int, std::allocator<int>>>>*)
void Descriptor::kMeansClustering(std::vector<std::vector<int>> *indices) {
	int k = 10;
//	int k = (int) sqrt(numPoints / 2);
	//initialize
	for (int i=0;i<k;i++) {
		std::vector<int> v;
		indices->push_back(v);
	}
	int* associations = new int[numPoints]();
	float* means = new float[k * dimension];
	int iterations = 0;

	//choose initial means
	for (int i=0;i<k;i++) {
		int index = rand() % numPoints;
		memcpy(means + i * dimension, data + index * dimension, dimension * sizeof(float));
	}

	bool update = true;
	while (update) {
		update = false;
		for (int i=0;i<k;i++)
			(*indices)[i].clear();

		//assignment step
		for (int i=0;i<numPoints;i++) {
			//find nearest mean
			int minIndex;
			float minDistance;
			for (int j=0;j<k;j++) {
				float distance = 0;
				for (int l=0;l<dimension;l++)
					distance += (data[i*dimension+l] - means[j*dimension+l]) * (data[i*dimension+l] - means[j*dimension+l]);
				if (j==0 || distance < minDistance) {
					minIndex = j;
					minDistance = distance;
				}
			}
			if (minIndex != associations[i])
				update = true;
			associations[i] = minIndex;
			(*indices)[minIndex].push_back(i);
		}

		//update step
		for (int i=0;i<k;i++) {
			for (int j=0;j<dimension;j++) {
				means[i * dimension + j] = 0;
				size_t clusterSize = (*indices)[i].size();
				for (size_t l=0;l<clusterSize;l++) {
					means[i * dimension + j] += data[(*indices)[i][l] * dimension + j];
				}
				means[i * dimension + j] /= clusterSize;
			}
		}
		iterations++;
	}

//	printf("K-Means: Found %lu clusters (%d iterations)\n",indices->size(),iterations);

	delete[] associations;
	delete[] means;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rsi, %rbx
movq %rdi, %r14
pushq $0xa
popq %rbp
leaq 0x20(%rsp), %r15
subl $0x1, %ebp
jb 0x87e8
xorps %xmm0, %xmm0
movaps %xmm0, 0x20(%rsp)
andq $0x0, 0x30(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6d22
movq %r15, %rdi
callq 0x2d00
jmp 0x87c0
movslq (%r14), %rax
leaq (,%rax,4), %r12
movq %rax, 0x8(%rsp)
testq %rax, %rax
pushq $-0x1
popq %r15
cmovsq %r15, %r12
movq %r12, %rdi
callq 0x2040
xorl %ebp, %ebp
movq %rax, 0x18(%rsp)
movq %rax, %rdi
xorl %esi, %esi
movq %r12, %rdx
callq 0x2100
movslq 0x4(%r14), %rax
imulq $0x28, %rax, %rdi
testq %rax, %rax
cmovsq %r15, %rdi
callq 0x2040
movq %rax, 0x10(%rsp)
cmpl $0xa, %ebp
je 0x8880
callq 0x2080
movl (%r14), %ecx
cltd
movq %rcx, 0x8(%rsp)
idivl %ecx
movslq 0x4(%r14), %rax
movl %eax, %ecx
imull %ebp, %ecx
movslq %ecx, %rcx
movq 0x10(%rsp), %rsi
leaq (%rsi,%rcx,4), %rdi
imull %eax, %edx
movslq %edx, %rsi
shlq $0x2, %rsi
addq 0x8(%r14), %rsi
shlq $0x2, %rax
movq %rax, %rdx
callq 0x2180
incl %ebp
jmp 0x8838
movb $0x1, %r13b
testb $0x1, %r13b
je 0x8a39
movq (%rbx), %rax
pushq $0x8
popq %rcx
cmpq $0xf8, %rcx
je 0x88b1
movq -0x8(%rax,%rcx), %rdx
cmpq %rdx, (%rax,%rcx)
je 0x88ab
movq %rdx, (%rax,%rcx)
addq $0x18, %rcx
jmp 0x8893
xorl %eax, %eax
xorl %r13d, %r13d
movq 0x8(%rsp), %rcx
movl %eax, 0x20(%rsp)
cmpl %ecx, %eax
jge 0x8981
movslq 0x4(%r14), %rcx
movl %ecx, %esi
imull %eax, %esi
testl %ecx, %ecx
movl $0x0, %edx
cmovgl %ecx, %edx
movslq %esi, %rsi
shlq $0x2, %rsi
addq 0x8(%r14), %rsi
shlq $0x2, %rcx
movq 0x10(%rsp), %rdi
xorl %r8d, %r8d
cmpq $0xa, %r8
je 0x8940
xorps %xmm0, %xmm0
xorl %r9d, %r9d
cmpq %r9, %rdx
je 0x891b
movss (%rsi,%r9,4), %xmm1
subss (%rdi,%r9,4), %xmm1
mulss %xmm1, %xmm1
addss %xmm1, %xmm0
incq %r9
jmp 0x88fd
testq %r8, %r8
sete %r9b
ucomiss %xmm0, %xmm2
seta %r10b
orb %r9b, %r10b
cmovnel %r8d, %ebp
jne 0x8935
movaps %xmm2, %xmm0
incq %r8
addq %rcx, %rdi
movaps %xmm0, %xmm2
jmp 0x88f1
cltq
movq 0x18(%rsp), %rdx
cmpl (%rdx,%rax,4), %ebp
setne %cl
orb %cl, %r13b
movl %ebp, (%rdx,%rax,4)
movslq %ebp, %rax
imulq $0x18, %rax, %rdi
addq (%rbx), %rdi
leaq 0x20(%rsp), %rsi
movss %xmm2, 0x8(%rsp)
callq 0x6d04
movss 0x8(%rsp), %xmm2
movl 0x20(%rsp), %eax
incl %eax
movl (%r14), %ecx
jmp 0x88bb
movq %rcx, 0x8(%rsp)
movslq 0x4(%r14), %rax
movq 0x8(%r14), %rcx
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
xorl %esi, %esi
cmpq $0xa, %rsi
je 0x8883
movq %rsi, %rdi
imulq %rax, %rdi
movq 0x10(%rsp), %r8
leaq (%r8,%rdi,4), %rdi
xorl %r8d, %r8d
cmpq %rdx, %r8
je 0x8a31
andl $0x0, (%rdi,%r8,4)
movq (%rbx), %r9
imulq $0x18, %rsi, %r11
movq (%r9,%r11), %r10
movq 0x8(%r9,%r11), %r9
subq %r10, %r9
sarq $0x2, %r9
leaq (%rcx,%r8,4), %r11
xorps %xmm0, %xmm0
xorl %r12d, %r12d
cmpq %r12, %r9
je 0x8a00
movslq (%r10,%r12,4), %r15
imulq %rax, %r15
addss (%r11,%r15,4), %xmm0
movss %xmm0, (%rdi,%r8,4)
incq %r12
jmp 0x89e2
testq %r9, %r9
js 0x8a0c
cvtsi2ss %r9, %xmm1
jmp 0x8a22
movq %r9, %r10
shrq %r10
andl $0x1, %r9d
orq %r10, %r9
cvtsi2ss %r9, %xmm1
addss %xmm1, %xmm1
divss %xmm1, %xmm0
movss %xmm0, (%rdi,%r8,4)
incq %r8
jmp 0x89b7
incq %rsi
jmp 0x899a
movq 0x18(%rsp), %rdi
callq 0x2270
movq 0x10(%rsp), %rdi
callq 0x2270
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x2d00
movq %rbx, %rdi
callq 0x23b0
nop